首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python美汤/ Find

Python美汤/ Find
EN

Stack Overflow用户
提问于 2020-04-22 10:42:22
回答 2查看 53关注 0票数 0

我的问题是,当我打印出链接列表时,它会在终端中打印一个漂亮的列表,但我不知道为什么链接( list )不包含find方法?而且,同样的代码也在我老师的ide上运行。

代码语言:javascript
运行
复制
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)
代码语言:javascript
运行
复制
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'**
EN

Stack Overflow用户

发布于 2020-04-22 11:00:59

迭代链接的"i“变量很可能没有"a”属性,也就是说,在html单元格中没有链接。也许你可以检查一下你是否真的有链接

代码语言:javascript
运行
复制
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 False
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61356402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档