简体   繁体   English

如何使用 Python 2 从网站上抓取数据?

[英]How to scrape data from a website using Python 2?

So when I run this code I keep getting empty brackets instead of the actual data.所以当我运行这段代码时,我不断得到空括号而不是实际数据。

I am trying to figure out why sense I don't receive any error messages.我想弄清楚为什么我没有收到任何错误消息。

import urllib
import re
symbolslist = ["aapl","spy","goog","nflx"]


for symbol in symbolslist:
    url = "http://finance.yahoo.com/q?s=%s&ql=1"%(symbol)
    htmlfile = urllib.urlopen(url)
    htmltext = htmlfile.read()
    regex = '<span id="yfs_184_%s">(.+?)</span>'%(symbol.lower())
    pattern = re.compile(regex)
    price = re.findall(pattern, htmltext)
    print price

括号出现是因为正则表达式的元素代码不是 184 它的 l84 它的 L 不是一个。

There is a number of libraries around which can help you to scrape sites.有许多库可以帮助您抓取站点。 Take a look at Scrapy or at Beautiful Soup they should support both Python 2 and 3 as far as I know.看看ScrapyBeautiful Soup ,据我所知,它们应该同时支持 Python 2 和 3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM