Python只会将大量数据写入文件

时间:2015-08-03 15:00:38

标签: python url web-scraping

该脚本是一个网络剪贴簿,它从信任交易者那里接收电子邮件 - 我需要它写入文本文件以及打印到控制台。这是代码,它只打印一个"批次"从网址到文件的数据?

from lxml import html
import requests
urls = ['http://www.trustatrader.com/search/?seed=&trade=1&where=Avon&page=1','http://www.trustatrader.com/search/?seed=&trade=1&where=Bedfordshire&page=1']
myfile = open('xyz.txt', 'a')
for url in urls:
    page = requests.get(url)
    tree = html.fromstring(page.text)
    emails = tree.xpath('//li[@class="email"]/a/@href')
    emails = [email.replace('mailto:','') for email in emails]
    emails = [email.replace(' ','') for email in emails]
    for email in emails:
        print str(email)+';'
        myfile.write("%s\n" % email)
myfile.close()

0 个答案:

没有答案