网络爬虫单词计数器
web crawler word counter
我正在使用一个简单的文本编辑器和 CMD 来 运行 我的 python 代码 我已经厌倦了构建一个网络爬虫,它会去一个网站并从中提取所有的单词但是当i 运行 它在 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/')
我有 运行 代码,正如 tobias 所指出的,Index_singleListingTitles
class 没有标签。我不知道您到底在找什么,但请尝试使用 google 开发人员工具或文本编辑器查看 ebay 页面源代码,看看是否可以找到它。
了解 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 with tag = h2 和属性 class = secret_flag.
我正在使用一个简单的文本编辑器和 CMD 来 运行 我的 python 代码 我已经厌倦了构建一个网络爬虫,它会去一个网站并从中提取所有的单词但是当i 运行 它在 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/')
我有 运行 代码,正如 tobias 所指出的,Index_singleListingTitles
class 没有标签。我不知道您到底在找什么,但请尝试使用 google 开发人员工具或文本编辑器查看 ebay 页面源代码,看看是否可以找到它。
了解 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 with tag = h2 和属性 class = secret_flag.