判断是否存在空值
from bs4 import *
import requests
head = {'user-agent':'betbargain/android-7.0/0.0.5'}
amaurl = input('Enter amazon url')
amazinfo = requests.get(amaurl,headers=head)
amasoup = BeautifulSoup(amazinfo.text,'lxml')
findResult = amasoup.find("span", attrs={"id":'productTitle'})
amatit = ""
if (findResult != None): # Added a check for findResult being None
amatit = str(findResult).strip() # Changed .string to str()
print(amatit)
该代码片段使用Python的requests库获取Amazon商品页面,然后用BeautifulSoup解析HTML,查找并打印出产品标题。如果在页面上找不到id为productTitle的span元素,则不会打印任何内容。
5万+

被折叠的 条评论
为什么被折叠?



