【问题标题】:Tracking frequency of words in an ebay search result跟踪 ebay 搜索结果中单词的频率
【发布时间】: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} 等等等等。

这是我使用的链接: http://www.ebay.com/sch/Car-Truck-Parts-/6030/i.html?_from=R40&LH_ItemCondition=4&LH_Complete=1&LH_Sold=1&_mPrRngCbx=1&_udlo=100&_udhi=700&_nkw=honda+%281990%2C+1991%2C+1992%2C+1993%2C+1994%2C+1995%2C+1996%2C+1997%2C+1998%2C+1999%2C+2000%2C+2001%2C+2002%2C+2003%2C+2004%2C+2005%29&_sop=16

我遇到的问题是我只获得前 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


    【解决方案1】:

    您将获得前 50 个结果,因为 EBay 为每个页面显示 50 个结果。解决方案是一次解析一页。通过此搜索,您可以使用不同的 url:

    http://www.ebay.com/sch/Car-Truck-Parts-/6030/i.html?_from=R40&LH_ItemCondition=4&LH_Complete=1&LH_Sold=1&_mPrRngCbx=1&_udlo=100&_udhi=700&_sop=16&_nkw=honda+%281990%2C+1991%2C+1992%2C+1993%2C+1994%2C+1995%2C+1996%2C+1997%2C+1998%2C+1999%2C+2000%2C+2001%2C+2002%2C+2003%2C+2004%2C+2005%29&_pgn=1&_skc=50&rt=nc

    注意到 url 中有一个参数_pgn=1?这是当前显示的页码。如果您提供的数字超过了搜索的页数,则会在类为"sm-md" 的 div 中出现错误消息

    所以你可以这样做:

    page = 1
    url = """http://www.ebay.com/sch/Car-Truck-Parts-/6030/i.html?_from=R40&LH_ItemCondition=4&LH_Complete=1&LH_Sold=1&_mPrRngCbx=1&_udlo=100&_udhi=700&_sop
      =16&_nkw=honda+%281990%2C+1991%2C+1992%2C+1993%2C+1994%2C+1995%2C+1996%2C+
      1997%2C+1998%2C+1999%2C+2000%2C+2001%2C+2002%2C+2003%2C+2004%2C+2005%29&
      _pgn="""+str(page)+"&_skc=50&rt=nc"
    
    has_page = True
    
    myfile = 'c:/users/' + myquery
    fw = open(myfile + '.xml', 'w')
    
    while has_page:
        r = requests.get(url)
        soup = BeautifulSoup(r.content, "lxml")
        error_msg = soup.find_all('p', {'class':"sm-md"})
        if len(error_msg) > 0:
            has_page = False
            continue
        for item in soup.find_all('ul',{'class':'ListViewInner'}):
            fw.write(str(item))
            page+=1
    
    fw.close()
    

    我只测试了进入页面和打印ul,效果不错

    【讨论】:

    • 太棒了!我认为它与页面有关,但我不确定,因为当我访问实际网站时,ebay 会显示 200 个列表。我可以问你是否知道我在哪里可以找到某种链接解码器?我还注意到链接中的某些参数似乎只在某些时候存在,并且我想找出原因。非常感谢您的帮助!
    • 我不知道任何链接解码器。检查 url 参数是您开始 Web 开发时会习惯的事情。该网站没有明确说明参数的作用,因为这些参数背后的逻辑是在后端制作的,但您可以使用它们看看会发生什么。参数在每个 url 上由 '&' 分隔。如果有帮助,您可以标记为已回答吗?祝你好运!
    猜你喜欢
    • 2021-01-16
    • 2016-03-24
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多