【问题标题】:Scrape html files stored in remote directory抓取存储在远程目录中的 html 文件
【发布时间】:2013-09-27 22:36:38
【问题描述】:

我在远程目录中存储了数千个 html 文件。所有这些文件都具有相同的 HTML 结构。现在我正在使用以下脚本手动抓取每个文件

from string import punctuation, whitespace
import urllib2
import datetime
import re
from bs4 import BeautifulSoup as Soup
import csv
today = datetime.date.today()
html = urllib2.urlopen("http://hostname/coimbatore/3BHK_flats_inCoimbatore.html_%94201308110608%94.html").read()

soup = Soup(html)
for li in soup.findAll('li', attrs={'class':'g'}):
    sLink = li.find('a')
    print sLink['href']
    sSpan = li.find('span', attrs={'class':'st'})
    print sSpan

所以上面的脚本是针对一个 URL 的。同样明智的是,我想浏览该目录下的所有 html 文件,而不管文件名如何。我发现没有人问过这个问题。

更新:代码

import urllib2
import BeautifulSoup
import re

Newlines = re.compile(r'[\r\n]\s+')

def getPageText(url):
    # given a url, get page content
 data = urllib2.urlopen(url).read()
    # parse as html structured document
 bs = BeautifulSoup.BeautifulSoup(data, convertEntities=BeautifulSoup.BeautifulSoup.HTML_ENTITIES)
    # kill javascript content
 for li in bs.findAll('li', attrs={'class':'g'}):
  sLink = li.find('a')
  print sLink['href']
  sSpan = li.find('span', attrs={'class':'st'})
  print sSpan
def main():
    urls = [
        'http://192.168.1.200/coimbatore/3BHK_flats_inCoimbatore.html_%94201308110608%94.html',
        'http://192.168.1.200/coimbatore/3BHK_flats_inCoimbatore.html_%94201308110608%94.html.html'
    ]
    txt = [getPageText(url) for url in urls]

if __name__=="__main__":
    main()    

【问题讨论】:

  • 获取目录下的文件列表,逐个处理。

标签: python python-2.7 beautifulsoup screen-scraping


【解决方案1】:

使用循环:

...

for url in url_list:
    html = urllib2.urlopen(url).read()

    soup = Soup(html)
    for li in soup.findAll('li', attrs={'class':'g'}):
        sLink = li.find('a')
        print sLink['href']
        sSpan = li.find('span', attrs={'class':'st'})
        print sSpan

如果你事先不知道url列表,你必须解析listing page。


import csv
import urllib2

import BeautifulSoup


def getPageText(url, filename):
    data = urllib2.urlopen(url).read()
    bs = BeautifulSoup.BeautifulSoup(data, convertEntities=BeautifulSoup.BeautifulSoup.HTML_ENTITIES)
    with open(filename, 'w') as f:
        writer = csv.writer(f)
        for li in bs.findAll('li', attrs={'class':'g'}):
            sLink = li.find('a')
            sSpan = li.find('span', attrs={'class':'st'})
            writer.writerow([sLink['href'], sSpan])

def main():
    urls = [
        'http://192.168.1.200/coimbatore/3BHK_flats_inCoimbatore.html_%94201308110608%94.html',
        'http://192.168.1.200/coimbatore/3BHK_flats_inCoimbatore.html_%94201308110608%94.html.html',
    ]
    for i, url in enumerate(urls, 1):
        getPageText(url, '{}.csv'.format(i))

if __name__=="__main__":
    main()    

【讨论】:

  • 见鬼,你的速度很快。很好的答案兄弟:)
  • @falsetru 。现在它如何做那个 URL 列表?我是否必须将所有 URL 存储在一个文件中?像下面这样urls = [ 'http://www.stackoverflow.com/questions/5331266/python-easiest-way-to-scrape-text-from-list-of-urls-using-beautifulsoup', 'http://stackoverflow.com/questions/5330248/how-to-rewrite-a-recursive-function-to-use-a-loop-instead' ]?
  • @Venky,在不知道 url 模式或列表页面结构的情况下,我无法回答。
  • @Venky,你从哪里得到 url 列表?
  • @falsetru 。我已经更新了使用两个 URL 的代码。我可以对其进行操作,以便将每个文件的输出存储在 diff .csv 文件中吗?让我知道是否有任何有效的方法来做我已经做过的事情。
猜你喜欢
  • 2013-04-16
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多