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

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

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()

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值