[英]Python beautiful soup/ Find
My Question is that when i print the links list out it prints a handsome list in the terminal but i don't know why the links(list) does not contain the find method?我的问题是,当我打印链接列表时,它会在终端中打印一个漂亮的列表,但我不知道为什么链接(列表)不包含 find 方法? Moreover this same code was working on my teachers's ide
此外,同样的代码正在我老师的 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'**
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")
It most likely the "i" variable iterating links does not have the attribute "a", that is, there isn't a link inside your html cell.很可能“i”变量迭代链接没有属性“a”,也就是说,html 单元格内没有链接。 Maybe it's you can check if you really have a link
也许你可以检查你是否真的有链接
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.