您的位置:首页 > 其它

beautifulsoup怎么查找子元素?与抓取活动行页面数据

2018-11-27 14:02 197 查看

活动行界面:http://www.huodongxing.com/events?orderby=o&city=%E5%85%A8%E9%83%A8&page=1

[code]import requests
import time
from bs4 import BeautifulSoup
page = 1
def getid():
global page
pages = requests.get('http://www.huodongxing.com/events?orderby=o&city=%E5%85%A8%E9%83%A8&page=' + str(page))
page = int(page) + 1
soup = BeautifulSoup(pages.text, 'html.parser')
res = soup.find_all("div",class_="search-tab-content-item-mesh") #查找所有class
for item in res: #循环res结果集
txtlist = item.find('a')  #找到第一个a标签
print(txtlist['href']) #打印a标签href值
time.sleep(5)
getid()
###############################分割线################################################
import requests
import time
from bs4 import BeautifulSoup
import pymysql
page = 1
def getid():
global page
while True:
pages = requests.get('http://www.huodongxing.com/events?orderby=o&city=%E5%85%A8%E9%83%A8&page=' + str(page))
soup = BeautifulSoup(pages.text, 'html.parser')
res = soup.find_all("div",class_="search-tab-content-item-mesh")
i=0
for item in res:
txtlist = item.find('a')
print('进行抓取第'+str(page)+'页,第'+str(i)+'个界面')
page2 = requests.get('http://www.huodongxing.com'+str(txtlist['href']))
soup2 = BeautifulSoup(page2.text, 'html.parser')
#标题
title = soup2.find('title').string
#宣传图片
images = soup2.find('div',class_="jumbotron media").find('img')['src']
#简介
summary = soup2.find('title').string
#内容
content = soup2.find('div', id="event_desc_page")
i = i+1
#数据入库
page = int(page) + 1
getid()

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: