Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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中的Web垃圾处理_Python_Python 3.x_Web Scraping_Python 3.5_Urllib - Fatal编程技术网

Python中的Web垃圾处理

Python中的Web垃圾处理,python,python-3.x,web-scraping,python-3.5,urllib,Python,Python 3.x,Web Scraping,Python 3.5,Urllib,以下代码输出空列表;我希望它能打印出股票价格。任何帮助都将不胜感激。谢谢 import urllib.request import re companyList = ["aapl","goog","nflx"] for i in range(len(companyList)): url = "https://finance.yahoo.com/quote/"+companyList[i]+"?p="+companyList[i] htmlfile = urllib.reques

以下代码输出空列表;我希望它能打印出股票价格。任何帮助都将不胜感激。谢谢

import urllib.request
import re
companyList = ["aapl","goog","nflx"]
for i in range(len(companyList)):

    url = "https://finance.yahoo.com/quote/"+companyList[i]+"?p="+companyList[i]
    htmlfile = urllib.request.urlopen(url)
    htmltext = htmlfile.read()
    regex = '<span class="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-reactid="35"><!-- react-text: 36 -->()(.+?)<!-- /react-text --></span>'
    pattern = re.compile(regex)
    price = re.findall(pattern, str(htmltext))
    print(price)

我将为其中一家公司做这件事。但我要你坚定地承诺,你不会告诉任何人我已经教过你怎么做

获取页面的HTML副本并将其保存在本地

>>> import urllib.request
>>> import re
>>> url = 'https://finance.yahoo.com/quote/AAPL/?p=AAPL'
>>> htmlfile = urllib.request.urlopen(url)
>>> htmltext = htmlfile.read()
>>> open('temp.htm', 'w').write(str(htmltext))
533900
检查页面,并复制粘贴您希望能够在此页面和类似页面中识别的项目。把它放在评论中以供参考

>>> # <span class="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-reactid="35"><!-- react-text: 36 -->161.38<
验证字符串中没有多个空白字符。如果有,则将整个空白字符串替换为\s+

>>> exp.find('  ')
-1
在字符串中对正则表达式重要的每个字符前面加上单个“\”字符

>>> re.sub(r'[().]', lambda m: '\\'+m.group(), exp)
'<span class="Trsdu\\(0\\.3s\\) Fw\\(b\\) Fz\\(36px\\) Mb\\(-4px\\) D\\(ib\\)" data-reactid="35"><!-- react-text: 36 -->161\\.38<'

看看下面的脚本是否有帮助。这也包括身份验证

    https://github.com/PraveenKandregula/JenkinsRSSScrappingWithPython/blob/master/JenkinsRSSScrappingWithPython.py

我看不出有多张反对票的意义,尤其是对新来者来说。也许你可以告诉我们你想从那页中提取什么。同时,我建议您最好使用BeautifulSoup,或者其他处理网页的方法,而不是regex。这种方法充满了困难。如果您想向我发送评论,请键入“@”符号以获得菜单并从列表中选择我的名字。请不要使用正则表达式解析HTML。你能看到这个著名的还是声名狼藉的?详细问答:@BillBell非常感谢您的建议。我是第一次尝试使用Webscraping,下面的教程使用了正则表达式。我肯定会在BeautifulSoup上换一个@cddt该链接非常有用。谢谢,我向你保证。非常感谢你的帮助D
>>> regex = '<span class="Trsdu\\(0\\.3s\\) Fw\\(b\\) Fz\\(36px\\) Mb\\(-4px\\) D\\(ib\\)" data-reactid="35"><!-- react-text: 36 -->([^<]+)<'
>>> re.findall(regex, str(htmltext))
['161.38']
    https://github.com/PraveenKandregula/JenkinsRSSScrappingWithPython/blob/master/JenkinsRSSScrappingWithPython.py