我现在正在做一个学习网站垃圾的项目 我选择了我的网站:
在这个页面上,底部有一个按钮,显示下一批10种产品的列表,而不单击此按钮。它不显示下一批产品,但是单击此按钮时URL不会更改
我想问一下如何使用requests模块解决这个难题
我的代码如下:
import requests
from bs4 import BeautifulSoup
r = requests.get("https://www.game.co.uk/en/m/games/best-selling-games/best-selling-xbox-one-games/?merchname=MobileTopNav-_-XboxOne_Games-_-BestSellers")
c = r.content
soup = BeautifulSoup(c,"html.parser")
all=soup.find_all("div",{"class":"product"})
for item in all:
print(item.find({"h2": "productInfo"}).text.replace('\h2','').replace(" ", ""))
print(item.find("span",{"class": "condition"}).text + " " + item.find("span",{"class": "value"}).text )
try:
print(item.find_all("span",{"class": "condition"})[1].text + " " + item.find_all("span",{"class": "value"})[1].text )
except:
print("No Preowned")
print(" ")
尝试此代码以获取该页中的所有可用项。您可以使用chromedev工具来检索this url,其中有一个页码递增选项
最后几个标题的输出:
您需要使用支持javascript/jquery执行的webcrawler—即selenium(它在引擎盖下使用BoutifulSoup) 您面临的问题是,当单击所提到的按钮时,您尝试访问的内容会通过javascript动态创建。 当您请求页面时,没有创建要从中读取的其他html元素,因此BoutifulSoup找不到它们。 使用selenium,您可以单击按钮/填写表单等等。也可以等待服务器创建要访问的内容
selenium的文档应该是自我解释的
相关问题 更多 >
编程相关推荐