【发布时间】:2021-02-12 11:07:24
【问题描述】:
我需要从网站上抓取内容(只是标题)。我为一页做了,但我需要为网站上的所有页面都做。 目前,我正在执行以下操作:
import bs4, requests
import pandas as pd
import re
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
r = requests.get(website, headers=headers)
soup = bs4.BeautifulSoup(r.text, 'html')
title=soup.find_all('h2')
我知道,当我转到下一页时,url 会发生如下变化:
website/page/2/
website/page/3/
...
website/page/49/
...
我尝试使用 next_page_url = base_url + next_page_partial 构建递归函数,但它没有移动到下一页。
if soup.find("span", text=re.compile("Next")):
page = "https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina/page/".format(page_num)
page_num +=10 # I should scrape all the pages so maybe this number should be changed as I do not know at the beginning how many pages there are for that section
print(page_num)
else:
break
我关注了这个问题(和答案):Moving to next page for scraping using BeautifulSoup
如果您需要更多信息,请告诉我。非常感谢
更新代码:
import bs4, requests
import pandas as pd
import re
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
page_num=1
website="https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina"
while True:
r = requests.get(website, headers=headers)
soup = bs4.BeautifulSoup(r.text, 'html')
title=soup.find_all('h2')
if soup.find("span", text=re.compile("Next")):
page = f"https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina/page/{page_num}".format(page_num)
page_num +=10
else:
break
【问题讨论】:
-
我相信 .format 行需要一个 {} 在你希望 page_num 去的字符串中。这有什么帮助吗?
-
page_num之前好像加了“page/”。 -
使用.format时,.format()中不需要加{}。
-
没有朋友,如
"website/page/{}".format(page_num)我认为虽然我个人使用 f 字符串所以它会是f"website/page/{page_num}" -
哦抱歉,太习惯f字符串了
标签: python web-scraping beautifulsoup web-crawler