gpt4 book ai didi

python - Cloudflare 抓取、查找元素

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:59 25 4
gpt4 key购买 nike

我一直在使用 cfscrape 模块,它允许您绕过网站上的 cloudflare 验证码保护...我已经访问了页面的内容,但似乎无法让我的代码工作,而是打印了整个 HTML。我只是想在 <span class="availability"> 中查找关键字

import urllib2
import cfscrape
from bs4 import BeautifulSoup
import requests
from lxml import etree
import smtplib
import urllib2, sys
scraper = cfscrape.CloudflareScraper()
url = "http://www.sneakersnstuff.com/en/product/25698/adidas-stan-smith-gtx"
req = scraper.get(url).content


try:
page = urllib2.urlopen(req)
except urllib2.HTTPError, e:
print("hi")
content = e.fp.read()


soup = BeautifulSoup(content, "lxml")
result = soup.find_all("span", {"class":"availability"})

我省略了一些不相关的代码部分

最佳答案

try:
page = urllib2.urlopen(req)
content = page.read()
except urllib2.HTTPError, e:
print("hi")

您应该读取包含 html 代码的 urlopen 对象。

并且您应该将 content 变量放在 except 之前。

关于python - Cloudflare 抓取、查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41406894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com