Python只向fi写入一批数据

2024-05-20 20:26:09 发布

您现在位置:Python中文网/ 问答频道 /正文

该脚本是一个网络刮刀,需要从信任交易员电子邮件-我需要它写入文本文件以及打印到控制台。这是代码,为什么它只打印一个“批”的数据从一个网址到文件

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()

Tags: inimportcomhttpforsearchemailhtml