【发布时间】:2015-01-26 14:58:32
【问题描述】:
我正在尝试从网站上抓取特定文本。因为我是 Python 新手,我发现使用单个脚本很难抓取文本,所以我先使用了这段代码:
import urllib
import requests
from bs4 import BeautifulSoup
htmltext = urllib.urlopen("https://io.winmasters.com/Feeds/api/event /282576?lang=el").read()
data = htmltext
soup = BeautifulSoup(data)
f = open('/Desktop/text.txt', 'w')
f.write(data)
f.close()`
接下来我正在尝试编写一个脚本来搜索文本并打印特定的单词。
with open("/Desktop/text.txt") as openfile:
for line in openfile:
for part in line.split():
if "odds=" in part:
print part
但是搜索脚本没有返回我正在搜索的文本。请问有什么建议吗?
【问题讨论】:
-
脚本返回什么以及您在搜索什么?
-
好地方。此外,该 URL 似乎返回 JSON,这非常方便,并且可以使用 Python
json库使用loads轻松解析。odds是 JSON 中的键之一,因此很容易找到所有值。 -
你有什么理由在那里有
import requests,然后还是使用urllib.urlopen- 似乎浪费了让事情变得简单的机会。