【问题标题】:Cloudflare scraping, finding elementsCloudflare 抓取,寻找元素
【发布时间】:2016-12-31 08:57:49
【问题描述】:

我一直在使用 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"})

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

【问题讨论】:

    标签: python html web-scraping beautifulsoup cloudflare


    【解决方案1】:
    try:
        page = urllib2.urlopen(req)
        content = page.read()
    except urllib2.HTTPError, e:
        print("hi")
    

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

    你应该把content 变量放在except 之前。

    【讨论】:

    • 您熟悉ConnectionError: ('Connection aborted.', BadStatusLine 错误吗?不知道为什么我会得到这个..
    • @ColeWorld 您应该发布其他问题,而不是在评论中提出新问题。请接受此答案以结束此问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多