【问题标题】:Scroll to next page and extract data滚动到下一页并提取数据
【发布时间】:2021-06-17 12:25:47
【问题描述】:

我正在尝试从“https://www.bbc.com/news/coronavirus”中提取最新更新中的所有正文

我已经成功地从第一页中提取了正文(50 个中的一个)。

我想滚动到下一页并再次执行此过程。

这是我写的代码。

from bs4 import BeautifulSoup as soup
import requests

links = []
header = []
body_text = []

r = requests.get('https://www.bbc.com/news/coronavirus')
b = soup(r.content,'lxml')

# Selecting Latest update selection
latest = b.find(class_="gel-layout__item gel-3/5@l")
# Getting title
for news in latest.findAll('h3'):
    header.append(news.text)
    #print(news.text)

# Getting sub-links
for news in latest.findAll('h3',{'class':'lx-stream-post__header-title gel-great-primer-bold qa-post-title gs-u-mt0 gs-u-mb-'}):
    links.append('https://www.bbc.com' + news.a['href'])

# Entering sub-links and extracting texts
for link in links:
    page = requests.get(link)
    bsobj = soup(page.content,'lxml')

    for news in bsobj.findAll('div',{'class':'ssrcss-18snukc-RichTextContainer e5tfeyi1'}):
        body_text.append(news.text.strip())
        #print(news.text.strip())

我应该如何滚动到下一页?

【问题讨论】:

    标签: web-scraping beautifulsoup python-requests web-crawler


    【解决方案1】:

    不知道你到底是什么文本,但你可以通过 api。

    import requests
    
    url = 'https://push.api.bbci.co.uk/batch'
    headers = {'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Mobile Safari/537.36'}
    
    for page in range(1,51):
        payload = '?t=%2Fdata%2Fbbc-morph-lx-commentary-data-paged%2Fabout%2F63b2bbc8-6bea-4a82-9f6b-6ecc470d0c45%2FisUk%2Ffalse%2Flimit%2F20%2FnitroKey%2Flx-nitro%2FpageNumber%2F{page}%2Fversion%2F1.5.4?timeout=5'.format(page=page)
    
        jsonData = requests.get(url+payload, headers=headers).json()
    
        results = jsonData['payload'][0]['body']['results']
        for result in results:
            print(result['title'])
            print('\t',result['summary'],'\n')
    

    【讨论】:

    • 你是如何链接到 BBC/Corona 病毒的,我无法完全理解这里发生了什么,你能解释一下吗?
    • 数据是通过他们的api获取的。您可以通过查看开发工具 (ctrl-shift-i) 并查看 Network -> XHR 来查看其来源。你会在那里看到数据,只是获取url和对应参数的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    相关资源
    最近更新 更多