Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-使用lxml-urllib2.urlopen变通方法从HTTPS url解析HTML将不起作用_Python_Python 2.7_Web Scraping_Lxml_Urllib2 - Fatal编程技术网

Python-使用lxml-urllib2.urlopen变通方法从HTTPS url解析HTML将不起作用

Python-使用lxml-urllib2.urlopen变通方法从HTTPS url解析HTML将不起作用,python,python-2.7,web-scraping,lxml,urllib2,Python,Python 2.7,Web Scraping,Lxml,Urllib2,我有一个以前工作的网页刮板。然而,该网站是专门设计用来抓取的,最近该网站改为使用HTTPS,这使得下面的相关代码摘录不再有效 from lxml import html url = 'http://www.blahblah.com' good_data = False while good_data ==False: try: data = html.fromstring(html.tostring(html.parse(

我有一个以前工作的网页刮板。然而,该网站是专门设计用来抓取的,最近该网站改为使用HTTPS,这使得下面的相关代码摘录不再有效

    from lxml import html

    url = 'http://www.blahblah.com'
    good_data = False
    while good_data ==False:
        try:
            data = html.fromstring(html.tostring(html.parse(url)))
            good_data=True
        except Exception:
            good_data=False
            print 'scraping failed'
            winsound.Beep(800,1000)
            time.sleep(pause)
以前数据被成功解析为“数据”的地方,现在我从lxml得到一个IOError

我读了一些书,尤其是在这里,我尝试使用urllib2实现给定的解决方案,如下所示:

    from lxml import html
    from urllib import urlopen

    url = 'http://www.blahblah.com'
    good_data = False
    while good_data ==False:
        try:
            data = html.fromstring(html.tostring(html.parse(urlopen(url))))
            good_data=True
        except Exception:
            good_data=False
            print 'scraping failed'
            winsound.Beep(800,1000)
            time.sleep(pause)
但现在我得到了错误

"TypeError: expected string or buffer".
无论我是从http还是从http中刮取,都会发生这种情况


无论是对原始问题的修复还是对原始问题的修复,我们都将不胜感激。

使用请求库获取html数据,并使用BeautifulSoup从html检索页面提取数据

您请求库从站点获取数据,这些数据将以HTML格式显示

import requests
url = 'http://www.google.com/search'
my_headers = { 'User-agent' : 'Mozilla/11.0' }
payload = { 'q' : 'pizza', 'start' : '0' }
r = requests.get( url, params = payload, headers = my_headers )
您可以使用BeautifulSoup库从检索到的数据(HTML数据)中提取任何类型的信息

现在,如果需要文本数据,可以使用此函数

soup.getText()
如果您想要标题或任何其他可以使用的标记信息,比如在给定的示例中,我检索了所有H3标记

h3tags = soup.find_all( 'h3', class_='r' )

您是否尝试过
请求
?刚刚尝试过。didr=requests.get(url)-我认为这是正确的。它没有给我任何错误,但实际上似乎并没有给我任何东西,即r.text、r.U内容都是空的。它必须欺骗Chrome用户代理才能正常工作。
h3tags = soup.find_all( 'h3', class_='r' )