我试着把这个页面,它有大约21000个产品
我的问题是如何得到所有的产品名称,形象和完整的类别层次结构的21000个产品。 图像和名称在同一页上,但类别在实际产品页中。
由于分页,我只能得到32个产品的标题和图像,这是在第一页
从首页获取标题的代码
import requests
from bs4 import BeautifulSoup
main_url = "https://paytmmall.com/fmcg-foods-glpid-101405?discoverability=online&use_mw=1"
import requests
result = requests.get(main_url)
print(result.text)
sp = BeautifulSoup(result.text,'html.parser')
print(sp.prettify())
getallTitle = [x.a.get('title') for x in sp.findAll("div", class_ = "_3WhJ")]
print(str(len(getallTitle )) + " fetched products Title")
print("/n")
print(getallTitle[2])
下面是如何处理分页问题。 分页只是按需发送请求,而不是立即获取请求。这意味着每次你点击任何一个页码,你都会看到一些根据网站设计的变化。 在您的例子中,url查询在每次单击任何页面链接时都会发生变化。生成的url是
如果你继续把页面=2改成你想刮的任何一个页面,你就可以抓取网站了。在
^{pr2}$您可以访问每个页面的json响应。但请记住,每页只有32个产品,这意味着您将请求659次。在
输出:
^{pr2}$编辑:
如果你想要层次结构,你需要转到每个产品的链接并把它拉出来。我提供了代码来实现这一点,但请记住,这将需要FORVER。假设每个请求大约需要2-3秒,则需要将近18个小时。在
输出:
或
如果所有产品都属于同一类别,那么您只需要获取第一个产品的类别,然后在遍历页面时应用于所有其他产品:
页面对pageone发出如下内容的请求(返回json)。看看你能不能改变参数来得到所有的结果
看起来你可以通过改变url来包含页面来改变referer头和正文中的当前页面
您可以从第一个请求中提取总结果计数
^{pr2}$您知道您正在成批请求32个(不过请尝试将此值增加到可能的最大值)。然后可以计算页面/请求的数量,然后在循环中发出。在
Python(第1页请求)
相关问题 更多 >
编程相关推荐