网络爬虫字计数器

时间:2016-02-18 12:55:10

标签: python web-crawler

我正在使用一个简单的文本编辑器和CMD来运行我的python代码我已经厌倦了构建一个网络爬虫,它将进入一个网站并从中提取所有单词但是当我在cmd中运行它时它没有显示任何单词和没有错误,它结束了。这是代码

import requests
from bs4 import BeautifulSoup
import operator

def start(url):
    word_list =  []
    source_code = requests.get(url).text
    soup = BeautifulSoup(source_code, 'html.parser')
    for post_text in soup.findAll('a',{'class':'Index_singleListingTitles'}):
        content = post_text.string
        words =  content.lower().split()
        for each_word in words:
            word_list.append(each_word)
    clean_up_list(word_list)


def clean_up_list(word_list):
    clean_word_list = []
    for word  in word_list:
        symbols = "!@#$%^&*()_+:\"<>?,./;[]-="
        for i in range(0, len(symbols)):
            word = word.replace(symbols[i], "")
        if len(word) > 0:
            print(word)
            clean_word_list.append(word)

start('http://www.ebay.com/')

2 个答案:

答案 0 :(得分:1)

我已经运行了代码,正如tobias指出的那样,Index_singleListingTitles类没有标记。我不知道你在寻找什么,但尝试使用谷歌开发者工具或文本编辑器查看易趣页面源代码,看看你是否能找到它。

答案 1 :(得分:0)

阅读HTML标签和属性。然后阅读您尝试提取单词的网站的源页面。

flag_finder = BeautifulSoup(get_with_cookie, "html.parser")
    for tag in flag_finder.find_all('h2', attrs = {"class": "secret_flag"}):

上面,我试图从 get_with_cookie 加载的HTML页面中捕获 flags ,其中tag = h2 和属性class = secret_flag

相关问题