【发布时间】:2021-09-26 00:23:00
【问题描述】:
我想检查是否有任何被排除的网站出现。我可以让它只在一个站点上工作,但是一旦我把它列在列表中,它就会在if donts in thingy 出错:
TypeError: 'in' 需要字符串作为左操作数,而不是元组"
这是我的代码:
import requests
from bs4 import BeautifulSoup
from lxml import html, etree
import sys
import re
url = ("http://stackoverflow.com")
donts = ('stackoverflow.com', 'stackexchange.com')
r = requests.get(url, timeout=6, verify=True)
soup = BeautifulSoup(r.content, 'html.parser')
for link in soup.select('a[href*="http"]'):
thingy = (link.get('href'))
thingy = str(thingy)
if donts in thingy:
pass
else:
print (thingy)
【问题讨论】:
-
因为 donts 是一个元组,它需要一个字符串。
-
我认为您的意思是
if thingy in donts:尽管if thingy not in donts:会比使用无操作 then 子句更直接。
标签: python parsing beautifulsoup