3zz*_*zzy 180 python screen-scraping
我想从网站上获取每日日出/日落时间.是否可以使用Python抓取Web内容?使用的模块是什么?有没有可用的教程?
小智 186
Use urllib2 in combination with the brilliant BeautifulSoup library:
import urllib2
from BeautifulSoup import BeautifulSoup
# or if you're using BeautifulSoup4:
# from bs4 import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen('http://example.com').read())
for row in soup('table', {'class': 'spad'})[0].tbody('tr'):
tds = row('td')
print tds[0].string, tds[1].string
# will print date and sunrise
Run Code Online (Sandbox Code Playgroud)
Sja*_*aak 60
我真的推荐Scrapy.
引用删除的答案:
- Scrapy爬行比机械化更快,因为使用异步操作(在Twisted之上).
- Scrapy在libxml2之上解析(x)html有更好,最快的支持.
- Scrapy是一个成熟的框架,具有完整的unicode,处理重定向,gzipped响应,奇数编码,集成的http缓存等.
- 进入Scrapy后,您可以在不到5分钟的时间内编写蜘蛛,下载图像,创建缩略图并将提取的数据直接导出到csv或json.
hoj*_*oju 16
我将从我的网络抓取工作中的脚本收集到这个位桶库中.
您案例的示例脚本:
from webscraping import download, xpath
D = download.Download()
html = D.get('http://example.com')
for row in xpath.search(html, '//table[@class="spad"]/tbody/tr'):
cols = xpath.search(row, '/td')
print 'Sunrise: %s, Sunset: %s' % (cols[1], cols[2])
Run Code Online (Sandbox Code Playgroud)
输出:
Sunrise: 08:39, Sunset: 16:08
Sunrise: 08:39, Sunset: 16:09
Sunrise: 08:39, Sunset: 16:10
Sunrise: 08:40, Sunset: 16:10
Sunrise: 08:40, Sunset: 16:11
Sunrise: 08:40, Sunset: 16:12
Sunrise: 08:40, Sunset: 16:13
Run Code Online (Sandbox Code Playgroud)
sco*_*ski 10
我强烈建议检查pyquery.它使用类似jquery(也称为css)的语法,这使得那些来自该背景的人非常容易.
对于您的情况,它将是这样的:
from pyquery import *
html = PyQuery(url='http://www.example.com/')
trs = html('table.spad tbody tr')
for tr in trs:
tds = tr.getchildren()
print tds[1].text, tds[2].text
Run Code Online (Sandbox Code Playgroud)
输出:
5:16 AM 9:28 PM
5:15 AM 9:30 PM
5:13 AM 9:31 PM
5:12 AM 9:33 PM
5:11 AM 9:34 PM
5:10 AM 9:35 PM
5:09 AM 9:37 PM
Run Code Online (Sandbox Code Playgroud)
您可以使用urllib2发出HTTP请求,然后您将拥有Web内容.
你可以这样得到它:
import urllib2
response = urllib2.urlopen('http://example.com')
html = response.read()
Run Code Online (Sandbox Code Playgroud)
Beautiful Soup是一个python HTML解析器,应该适用于屏幕抓取.
特别是,这是他们解析HTML文档的教程.
祝好运!
归档时间: |
|
查看次数: |
189280 次 |
最近记录: |