【发布时间】:2015-12-28 01:20:50
【问题描述】:
使用 Python 3.5,我想做的是通过生成链接进入 ebay 搜索的结果页面,将源代码保存为 XML 文档,并遍历每个单独的列表,其中可能有 1000 个或更多。接下来,我想创建一个字典,其中包含每个列表标题中出现的每个单词(仅标题)及其相应的出现频率。例如,如果我搜索“honda civic”,其中 30 个结果是“honda civic 点火开关”,我希望我的结果显示为
results = {'honda':70, 'civic':60, 'igntion':30, 'switch':30, 'jdm':15, 'interior':5}
等等等等。
我遇到的问题是我只获得前 50 个结果,而不是使用不同的搜索选项可能会获得的 X,000 个结果。有什么更好的方法来解决这个问题?
还有我的代码:
import requests
from bs4 import BeautifulSoup
from collections import Counter
r = requests.get(url)
myfile = 'c:/users/' + myquery
fw = open(myfile + '.xml', 'w')
soup = BeautifulSoup(r.content, 'lxml')
for item in soup.find_all('ul',{'class':'ListViewInner'}):
fw.write(str(item))
fw.close()
print('...complete')
fr = open(myfile + '.xml', 'r')
wordfreq = Counter()
for i in fr:
words = i.split()
for i in words:
wordfreq[str(i)] = wordfreq[str(i)] + 1
fw2 = open(myfile + '_2.xml', 'w')
fw2.write(str(wordfreq))
fw2.close()
【问题讨论】:
标签: python python-3.x beautifulsoup xml-parsing