如何从bs4 python获得标题,这是instagram的粉丝总数
我正在使用此代码提取跟随者编号如何从bs4 python获得标题,这是instagram的粉丝总数,python,selenium,web-scraping,beautifulsoup,automation,Python,Selenium,Web Scraping,Beautifulsoup,Automation,我正在使用此代码提取跟随者编号 driver.get("https://www.instagram.com/test/?hl=en") driver.implicitly_wait(5) htmlcontent = driver.page_source soup = BeautifulSoup(htmlcontent, 'html.parser') followers = (soup.find_all('a', class_='-nal3')) 当我打印followers时
driver.get("https://www.instagram.com/test/?hl=en")
driver.implicitly_wait(5)
htmlcontent = driver.page_source
soup = BeautifulSoup(htmlcontent, 'html.parser')
followers = (soup.find_all('a', class_='-nal3'))
当我打印followers时,它会给出这个输出,
不,我无法提取标题有人请帮忙
[<a class="-nal3" href="/test/followers/" tabindex="0"><span class="g47SY" title="1,001,318">1m</span> followers</a>, <a class="-nal3" href="/test/following/" tabindex="0"><span class="g47SY">588</span> following</a>]
[,]
如果要从汤返回的HTML代码中获取属性,必须使用[]
括号,并在括号之间输入一个字符串,该字符串是要获取值的键的名称。在你的情况下,它将是
print(i['title'])
还有第二种方法,它的工作方式类似于try
子句-如果标题不在元素中,它将避免错误
print(i.get('title'))
你忘记关闭你的[]
应该是i['title']
而不是i['title'
我在这张打印中是什么(i['title'])
它给出了错误“raise AttributeError(AttributeError:ResultSet对象没有属性“i”)。你可能把一个项目列表当作一个单独的项目。你是否调用了find_all()当你打算调用find()?`@farhanAlam时,你已经编辑了这篇文章。现在,如果你想提取所有标题,只需使用[x.get('title)for x in followers]
,请重新接受我的答案。@AleksanderIkleiw我没有从这个[x.get('title)for x in followers]
中得到任何东西,我只在响应中得到了=[None,None]
请尝试[x['title']以获得追随者中的x]