【发布时间】:2019-05-25 11:27:23
【问题描述】:
我正在编写一个程序来检测带有打印的 404 页面。要检测这些页面,我会使用数组列表中的名称来填充 url,例如 --> steamcommunity.com/groups/(ARRAY FILLED)。
from bs4 import BeautifulSoup
import requests
import json
names = json.loads(open('names.json').read())
def groupfinder():
for name in names:
url = requests.get('https://steamcommunity.com/groups/').text + name
soup = BeautifulSoup(url, 'lxml')
clan = soup.find('span', class_='grouppage_header_abbrev')
clantag = clan
if clan != None:
print(clantag.text,"is already taken")
else:
print('GROUP FOUND',name)
groupfinder()
for 循环中的代码应该在每个数组名称上运行,但它只是坚持 else 语句。当所有组在域中时,它会输出所有组都已找到。
soup find 正在搜索所有声明的 url 所具有的组的名称。我正在寻找一个无人认领的。
【问题讨论】:
-
旁注,更pythonic应该是
if clan is not None -
如果只有
else会触发,那是因为if-test,无论您期望什么,总是错误。换句话说,soup.find()在您认为不应该返回的情况下返回[]。在soup.find()调用之后添加一行print(clan),这样您就可以看到发生了什么。
标签: python loops beautifulsoup