【发布时间】:2018-06-05 09:51:12
【问题描述】:
我一直在尝试通过登录(使用 yelp)来抓取网站。为了更好地理解的第一个问题:我遵循了一些教程来获得这些想法,并注意到它们都使用 CSRF 令牌制作字典,但是,当我抓取 yelp 登录站点时,我发现了 6 个令牌。我知道我的字典中不能有重复的键,所以本教程使用字典来处理这种冗余/不正确的情况,因为我只会得到最后一个标记?
其次,如果有多个令牌,你使用哪个?或者你如何使用它们?我似乎无法登录,并阅读了 BeautifulSoup 和 Requests 的文档,并在昨晚搜索了 Stack。代码如下。 感谢您的任何解释。
s = requests.session()
login = s.get('https://www.yelp.com/login')
soup = BeautifulSoup(login.text, 'html.parser')
tokenList = soup.find_all(type = 'hidden', attrs={"name": "csrftok"})
c = login.cookies #Just peeked into cookies to see if there is a token
print(c)
keys = [x.attrs["name"] for x in tokenList]
values = [x.attrs["value"] for x in tokenList]
#If I print these two lists, I get 6 keys of the "csrftok" String, and 6
#different keys.
email = "my email"
password = "my password"
#I tried creating a dictionary with zip of all the tokens, etc. This
#is an attempt just using the first key and value I find.
d = {'email': email, 'password': password, keys[0]: values[0]}
response = s.post('https://www.yelp.com/login', data = d)
print(response.url)
【问题讨论】:
标签: python web-scraping beautifulsoup csrf