Python 当url更改并添加“offset=[here]”时,Web抓取多个页面
上面的代码得到49条记录,这是第一页。我想刮43页。每次您转到下一页获取下50个视频时,最初从第一页到第二页的url都会增加?offset=150,然后每一页都会增加100。下面是最后一页url的示例,您可以看到偏移量=4250Python 当url更改并添加“offset=[here]”时,Web抓取多个页面,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,上面的代码得到49条记录,这是第一页。我想刮43页。每次您转到下一页获取下50个视频时,最初从第一页到第二页的url都会增加?offset=150,然后每一页都会增加100。下面是最后一页url的示例,您可以看到偏移量=4250 关于如何获得所有页面的结果集的任何帮助都将非常有用。谢谢我在Reelgood工作。请注意,每次我们发布web应用程序更新时,上的类名都会更改 我们非常乐意帮助您完成任何您想要完成的任务,请随时向我发送电子邮件luigi@reelgood.com.我想最简单的方法就是抓取
关于如何获得所有页面的结果集的任何帮助都将非常有用。谢谢我在Reelgood工作。请注意,每次我们发布web应用程序更新时,上的类名都会更改
我们非常乐意帮助您完成任何您想要完成的任务,请随时向我发送电子邮件luigi@reelgood.com.我想最简单的方法就是抓取更多内容的链接所在的class='eH' 它是页面上唯一具有该值的类。当达到偏移量=4250时,链接消失 所以循环是这样的:
from bs4 import BeautifulSoup
import pandas as pd
import requests
r = requests.get('https://reelgood.com/source/netflix')
soup = BeautifulSoup(r.text, 'html.parser')
title = soup.find_all('tr',attrs={'class':'cM'})
records = []
for t in title:
movie = t.find(attrs={'class':'cI'}).text
year = t.find(attrs={'class':'cJ'}).findNext('td').text
rating = t.find(attrs={'class':'cJ'}).findNext('td').findNext('td').text
score = t.find(attrs={'class':'cJ'}).findNext('td').findNext('td').findNext('td').text
rottenTomatoe = t.find(attrs={'class':'cJ'}).findNext('td').findNext('td').findNext('td').findNext('td').text
episodes = t.find(attrs={'class':'c0'}).text[:3]
records.append([movie, year, rating, score, rottenTomatoe, episodes])
df = pd.DataFrame(records, columns=['movie', 'year', 'rating', 'score', 'rottenTomatoe', 'episodes'])
是这样的吗?但是,如果你不介意我问你是如何找到url_标签类的,我会用它来代替标题中的t来看看soup?
records = []
keep_looping = True
url = "https://reelgood.com/source/netflix"
while keep_looping:
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
# grab your content here and store it and find the next link to visit.
title = soup.find....
for t in title:
....
records.append...
# if the tag does not exist, url will be None
# we will then tell the while-loop to stop by setting the keep_looping flag to False"
url_tag = soup.find('a', class_='eH')
# returns not absolute urls but "/source/netflix?offset=150"
if not url_tag:
keep_looping = False
else:
url = "https://www.reelgood.com" + url_tag.get('href')
df = pd.DataFrame...