【发布时间】:2016-12-18 02:37:00
【问题描述】:
我正在使用 Beautiful Soup 在 Python 中制作网络爬虫。我想从某个 div 获取链接,我现在的代码不打印任何东西。
import requests
from bs4 import BeautifulSoup
def spider(max_pages):
page = 1
while page <= max_pages:
url = 'https://thenewboston.com/'
source = requests.get(url)
plain_text = source.text
obj = BeautifulSoup(plain_text, "html5lib")
for link in obj.find_all('div', {'class': 'videos-top-courses'}):
href = 'https://thenewboston.com/', link.get('href')
print(href)
page += 1
spider(1)
【问题讨论】:
-
在这个链接里看答案希望对你有帮助=stackoverflow.com/questions/28390593/…
-
<div>没有href- 你必须找到<a> -
没有('div', {'class': 'videos-top-courses'}),查看源码
-
查看您正在访问的 URL 中的 HTML,该类没有
<div>s - 只有<table>元素,因此您的find_all调用(正确)不返回任何元素. -
没有
<div>和class="videos-top-courses"。有<table>和class="videos-top-courses"
标签: python request beautifulsoup web-crawler bs4