【发布时间】:2014-09-02 02:19:43
【问题描述】:
问题:我正在尝试使用beautifulsoup 仅针对可见文本抓取多个网站,然后将所有数据导出到单个文本文件中。
此文件将用作使用 NLTK 查找搭配的语料库。到目前为止,我正在处理这样的事情,但任何帮助都将不胜感激!
import requests
from bs4 import BeautifulSoup
from collections import Counter
urls = ["http://en.wikipedia.org/wiki/Wolfgang_Amadeus_Mozart","http://en.wikipedia.org/wiki/Golf"]
for url in urls:
website = requests.get(url)
soup = BeautifulSoup(website.content)
text = [''.join(s.findAll(text=True))for s in soup.findAll('p')]
with open('thisisanew.txt','w') as file:
for item in text:
print(file, item)
不幸的是,这有两个问题:当我尝试将文件导出到 .txt 文件时,它完全是空白的。
有什么想法吗?
【问题讨论】:
-
超时?你能显示超时的输出是什么吗? “出口不起作用”是什么意思?有什么错误吗?谢谢。
-
找出了“超时”部分并编辑了代码以反映它!至于“导出不起作用”的部分,我的意思是它返回一个空白文档!
-
你能不能修复你的缩进?谢谢。
-
好的,抱歉没看到应该都设置好了
标签: python python-3.x beautifulsoup