【问题标题】:Generating URL for Yahoo and Bing Scraping for multiple pages with Python and BeautifulSoup使用 Python 和 BeautifulSoup 为 Yahoo 和 Bing 生成 URL 抓取多个页面
【发布时间】:2021-02-01 18:29:54
【问题描述】:

我想从不同来源抓取新闻。我找到了一种生成 URL 以从 google 抓取多个页面的方法,但我认为有一种方法可以生成更短的链接。

你能告诉我如何生成用于抓取 Bing 和 Yahoo 新闻的多个页面的 URL,还有,有没有办法使 google url 更短。

这是谷歌的代码:

from bs4 import BeautifulSoup
import requests

headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}

term = 'usa'
page=0

for page in range(1,5):

    page = page*10

    url = 'https://www.google.com/search?q={}&tbm=nws&sxsrf=ACYBGNTx2Ew_5d5HsCvjwDoo5SC4U6JBVg:1574261023484&ei=H1HVXf-fHfiU1fAP65K6uAU&start={}&sa=N&ved=0ahUKEwi_q9qog_nlAhV4ShUIHWuJDlcQ8tMDCF8&biw=1280&bih=561&dpr=1.5'.format(term,page)
    print(url)

    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')

这些是 yahoo 和 bing 的 URL-s,但只有 1 页:

雅虎:url = 'https://news.search.yahoo.com/search?q={}'.format(term) 必应:url = 'https://www.bing.com/news/search?q={}'.format(term)

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    我不确定你是否在关注这个用于新闻的缩短网址。

    from bs4 import BeautifulSoup
    import requests
    
    headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
    
    term = 'usa'
    page=0
    
    for page in range(1,5):
    
        page = page*10
    
        url = 'https://www.google.com/search?q={}&tbm=nws&start={}'.format(term,page)
        print(url)
    
        response = requests.get(url, headers=headers,verify=False)
        soup = BeautifulSoup(response.text, 'html.parser')
    

    #雅虎:

    from bs4 import BeautifulSoup
    import requests
    
    headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
    
    term = 'usa'
    page=1
    while True:
    
        url ='https://news.search.yahoo.com/search?q={}&pz=10&b={}'.format(term,page)
        print(url)
        page = page + 10
        response = requests.get(url, headers=headers,verify=False)
        if response.status_code !=200:
            break
        soup = BeautifulSoup(response.text, 'html.parser')
    

    【讨论】:

    • 谢谢,但是如何为 Bing 和 Yahoo 生成多个页面的 url?
    • 你能分享一下你为雅虎和宾果尝试过的网址吗?
    • 我已经更新了问题,检查一下。我已经添加了网址
    • @taga : 找不到宾果游戏废页的任何参数。
    猜你喜欢
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多