(新问题)Python BeautifulSoup如何在保持向下滚动的同时捕获文本?(网络爬虫)

2024-04-25 14:13:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经解决了前面的问题。但现在我面临一个新问题。 当我在页面中搜索Sheeran时,if "Shreeran" in j:一切都很好。但是,如果我再添加一个像concert这样的关键字,结果将随机生成。例如;if "Shreeran" or "concert" in j:。我该怎么修?你知道吗

while True:
    url ='https://xxxxxxxxx/{}'.format(pagenum)
    driver.get(url)
    pagesource = driver.page_source
    soup = BeautifulSoup(pagesource, 'lxml')
    if url == "https://xxxxxxxxxx/5":
        break
    else:
        for s in soup.find_all("div", class_="_2cNsJna0_hV8tdMj3X6_gJ"):
            for j in s:
                if "Sheeran" in j: # only search Sheeran is fine but if i change it to "Sheeran" or "concert", the result will be generated randomly
                    print(s.text)


    pagenum+=1

    time.sleep(2)

我怎样才能用多个关键字搜索一些东西?你知道吗


Tags: orinhttpsurlforifdriver关键字
1条回答
网友
1楼 · 发布于 2024-04-25 14:13:47

另一种方法是找出网站在滚动时是如何获取内容的。你知道吗

您可以尝试在循环中增加页码。你知道吗

pagenum = 1
while True:
    url ='https://lihkg.com/thread/1082050/page/{}'.format(pagenum)
    driver.get(url)
    pagesource = driver.page_source
    soup = BeautifulSoup(pagesource, 'lxml')
    profile_links = soup.find('a', attrs={'href': re.compile('/profile'))
    if not profile_links:
        break
    pagenum+=1
    # page is valid, continue with code to extract results 

或者使用出现在网络流量中的API url。你知道吗

相关问题 更多 >