繁体   English   中英

当 url 更改并添加 'offset=[# here]' 时,Web 抓取多个页面

[英]Web scraping multiple pages when the url changes and adds 'offset=[# here]'

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'])

上面的代码让我得到了 49 条记录,这是第一页。 我想刮 43 页。 每次转到下一页以获取接下来的 50 个视频时,最初从第一页到第二页的 url 都会添加“?offset=150”,然后在它之后的每一页都增加 100。这是 url 外观的示例就像最后一页一样(你可以看到 offset=4250)“ https://reelgood.com/source/netflix?offset=4250

关于如何获得所有页面的结果集的任何帮助都会非常有帮助。 谢谢你

我想最简单的方法就是获取更多内容链接所在的 class='eH'。

它是页面上唯一具有该值的类。 当您到达 offset=4250 时,链接消失了。

所以循环会是这样的:

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...

我在雷尔古德工作。 请注意,每次我们发布 Web 应用程序更新时, https: //reelgood.com 上的类名称都会更改。

我们非常乐意为您在这里尝试完成的任何事情提供帮助,请随时通过 luigi@reelgood.com 向我发送电子邮件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM