【发布时间】: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