0

我正在尝试从网站上抓取数据www.vestiairecollective.com而抓取时我只能访问它的几个主页。例如,我的脚本无法抓取 url 的数据http://www.vestiairecollective.com/women-bags/handbags/#_=catalog&id_brand%5B%5D=50&material%5B%5D=3&step=180

我已经提到了许多关于堆栈溢出的问题,这些问题展示了如何做到这一点。当我在 Windows 上使用 python 3.5 时,“mechanize”和“cookielib”不起作用。我还看到一些问题指出像“robobrowser”这样的库可以完成这项工作。我也试过了,结果卡在了中间。

然后我尝试使用会话,当我使用 request.Sessions() 键入时,它说请求没有称为会话的属性。

当我使用上述 URL 时,请使用 robobrowser 或任何其他方式帮助我处理此特定网站的代码。

这是我在参考答案后尝试过的:-

import urllib.request
from bs4 import BeautifulSoup
import requests
session=requests.Session()
loginUrl='http://www.vestiairecollective.com/'
resLogin=session.post(loginUrl,data=  {'h':'5fcdc0ac04537595a747e2830037cca0','email':'something@gmail.com','password':'somepasswrd','ga_client_id':'750706459.1463098234'})
url='http://www.vestiairecollective.com/women-bags/handbags/#_=catalog&id_brand%5B%5D=50&material%5B%5D=3'
res=session.get(url)
//The below url i have given because I want to scrape from this url
crl=urllib.request.urlopen("http://www.vestiairecollective.com/women-bags/handbags/#_=catalog&id_brand%5B%5D=50&material%5B%5D=3")

soup=BeautifulSoup(crl.read(),"html.parser")

geturl=soup.find_all("div",{"class":"expand-snippet-container"})    

for i in geturl:           //The Scraping Part
    data1=i.find_all("p",{"class":"brand"})
    datac1=[da.contents[0] for da in data1]
    brdata=("\n".join(datac1))
    print(brdata)

这里应该从“crl”页面进行抓取,但从主页本身进行。

4

1 回答 1

0

你有一个错误request.Sessions()应该是request.Session()

我对类似问题的回答将提供一些使用 python requests(Python 3) 持久登录的示例代码。

简要总结:

  • 使用requests模块创建会话
  • 您可以使用postget参数登录自己
  • 会话对象的进一步请求将适当地处理 cookie
  • 确保你使用了一些真实的用户代理(否则一些网站不会让你登录,因为他们认为你的脚本是一个机器人)

一些相关的代码行可以激发您的灵感(不是按原样工作,您需要根据需要修改这些代码):

import requests
session = requests.Session()
session.headers.update({'user-agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1')
# use the site's form field names for the following line
# (and use the resLogin for checking successful login):
resLogin = session.post(loginUrl, data = {'user' : 'username', 'password' : 'pwd'}) 
# follow-up calls to a session which was used to login
res = session.get(url)
于 2016-06-03T06:50:12.543 回答