![](/img/trans.png)
[英]Web scraping : not able to scrape text and href for a given div, class
[英]Web scraping: Unable to loop into div element with class to get text and URL
我正在嘗試抓取一個使用div
和class
的網站來獲取其中的內容。
我能夠獲得正確的數據,但是當我將其放入循環時會出錯。
html = BeautifulSoup(response, 'html.parser')
post_list = html.find_all('div', class_='eodLhs')
print(post_list)
i = 0
for values in post_list:
url_json = {'title': values.ul.li[i].a.text, 'url': values.ul.li[i].a['href']}
names.append(values.ul.li[i].a.text)
i = i+1
Output of the print statement is: https://gist.github.com/parikhparth23/48669444506502f11409d43b30a4250d
它在這一行拋出錯誤:
url_json = {'title': values.ul.li[i].a.text, 'url': values.ul.li[i].a['href']}
我想在抓取后獲取文本和 URL。
根據您的要點,我認為您可以只使用 css 選擇器,以確保您在父 class 中有子 href。 在您現有的代碼中, i 增量應該發生在循環中,但如果您按照我的描述重寫,則不需要。 使用以運算符開頭的屬性值來刪除共享鏈接,因為我懷疑您只想要內容的原始鏈接
for i in soup.select(".eodLhs [href^='/']"):
print({i.text:i['href']})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.