我的问题是,当我打印出链接列表时,它会在终端中打印一个漂亮的列表,但我不知道为什么链接( list )不包含find方法?而且,同样的代码也在我老师的ide上运行。
import requests
from bs4 import BeautifulSoup
param = {'s': 'zombie'}
r = requests.get('http://chilltime.pk/search', params=param
soup = BeautifulSoup(r.text, "html.parser")
results = soup.find('tbody')
links = soup.findAll('td')
for i in links:
item_text = i.find('a').text
item_href = i.find('a').attrs['href']
if item_text and item_href:
print(item_text)
print(item_href)ERROR:
**Traceback (most recent call last):
File "C:/Users/AFFAN ULHAQ/PycharmProjects/Beautiful/bsp.py", line 19, in <module>
item_text = i.find('a').text
AttributeError: 'NoneType' object has no attribute 'text'**发布于 2020-04-22 10:54:51
import requests
from bs4 import BeautifulSoup
params = {
's': 'zombie'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
}
def main(url):
r = requests.get(url, params=params, headers=headers)
soup = BeautifulSoup(r.content, 'html.parser')
target = soup.findAll("a", href=True)
for tar in target:
print(tar.text, tar['href'])
main("http://chilltime.pk/search")发布于 2020-04-22 11:00:59
迭代链接的"i“变量很可能没有"a”属性,也就是说,在html单元格中没有链接。也许你可以检查一下你是否真的有链接
for i in links:
item_text = i.find('a').text if i.find('a') else False
item_href = i.find('a').attrs['href'] if i.find('a') else Falsehttps://stackoverflow.com/questions/61356402
复制相似问题