【发布时间】:2016-03-27 22:00:21
【问题描述】:
所以我需要从“计算机科学”维基百科页面获取前 10 个链接。然后我需要为 CS 页面中的每个链接获取 10 个链接。所以我最终会有 10*10 = 100 个链接。
直到现在我写了这段代码:
import urllib.request as urllib2
html = urllib2.urlopen('https://en.wikipedia.org/wiki/Computer_science').read()
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, "lxml")
for link in soup.find_all('a', limit=10):
rez=link.get('href')
for i in rez.find_all('a', limit=10):
print(i)
当我运行它时,我得到了这个错误:
“NoneType”对象没有“find_all”属性
谢谢,这很有帮助。接下来我需要从每个返回的链接中获取 10 个链接,即来自 Programming_language_theory、Computational_complexity_theory.. 等的 10 个链接。我尝试这样做:
for link in soup.find_all('a', href=True, title=True, limit=10):
print(link['href'])
for link2 in link['href'].find_all('a', href=True, title=True, limit=10):
print(link2['href'])
但我收到一个错误:“str”对象没有属性“find_all”
【问题讨论】:
标签: parsing python-3.x html-parsing wikipedia