Python 使用BeautifulSoup移动到下一页进行刮削
我需要从网站上抓取内容(只是标题)。我做了一个页面,但我需要做的网站上的所有页面。 目前,我的工作如下:Python 使用BeautifulSoup移动到下一页进行刮削,python,web-scraping,beautifulsoup,web-crawler,Python,Web Scraping,Beautifulsoup,Web Crawler,我需要从网站上抓取内容(只是标题)。我做了一个页面,但我需要做的网站上的所有页面。 目前,我的工作如下: 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.
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')
website/page/2/
website/page/3/
...
website/page/49/
...
我知道,当我移动到下一页时,url的变化如下:
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')
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
我跟着这个问题(和答案):
如果你需要更多信息,请告诉我。非常感谢
更新代码:
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
如果使用f“url/{page\u num}”
则删除格式(page\u num)
您可以在下面使用任何您想要的内容:
page=f”https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina/page/{page_num}“
或
page=”https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina/page/{}.格式(页码)
祝你好运
最后的答案是:
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")):
website = f"https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina/page/{page_num}"
page_num +=1
else:
break
.format行需要一个{}在您希望page_num进入的字符串中,我相信。这有什么帮助吗?在
page_num
之前,似乎添加了“page/”。当你使用.format时,你不需要在.format()中添加{}。没有朋友,就像“website/page/{}”。format(page_num)
我个人认为我使用了f字符串,所以它应该是f“website/page/{page_num}”
哦,抱歉,也习惯于f字符串,很抱歉我没有检查。您已将url更新到页面
,但在请求中使用了网站
。请检查。更改后的报废url存储在页面
中,但当while
再次启动时,r=requests()
仍从网站
获取数据。请尝试在条件下将页面
更改为网站
。您仍然有。当您使用f”“
时,应该删除格式(page\u num)
,page\u num+=1
将是答案。在第一次迭代时,将使用初始化的网站
。在此之后,第二次迭代时,page_num
将为2,因为1被添加到初始值1中。我更新了我的答案。请看一看,让我知道有一个问题。