如何在多个网站上迭代并使用web解析文本

2024-03-29 14:42:49 发布

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

我正在尝试解析文本,并对来自多个网站的文本进行情感分析。我已经成功地一次只能删除一个网站,并使用TextBlob库生成一个情感分数,但我正在尝试在许多网站上复制这一点,有什么想法可以从哪里开始?在

代码如下:

import requests
import json
import urllib
from bs4 import BeautifulSoup
from textblob import TextBlob


url = "http://www.reddit.com/r/television/comments/38dqxf/josh_duggar_confessed_to_his_father_jim_bob/"
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)

# kill all script and style elements
for script in soup(["script", "style"]):
    script.extract()    # rip it out

# get text
text = soup.get_text()

# break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
# break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
# drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)

#print(text)

wiki = TextBlob(text)
r = wiki.sentiment.polarity

print r

提前谢谢你


Tags: andtextin文本importfor网站line
1条回答
网友
1楼 · 发布于 2024-03-29 14:42:49

这是通过python中的URL从网站获取数据的方法:

import urllib2
response = urllib2.urlopen('http://reddit.com/')
html = response.read()

html是一个字符串,包含来自URL的所有HTML。在

我不太清楚你想从每一页中得到什么。如果你在下面评论,我可以编辑这个答案,并帮助你进一步。在

编辑:

如果要遍历URL列表,可以创建一个函数并按如下方式执行:

^{pr2}$

相关问题 更多 >