我最近看了一个用python编写web爬虫的视频。不知为什么,我收到了一个错误。我试着用代码的第6行来修复它,但是没有成功。知道它为什么会出错吗?代码是从到newboston逐字记录的。在
import requests
from bs4 import BeautifulSoup
def creepy_crawly(max_pages):
page = 1
#requests.get('https://www.thenewboston.com/', verify = True)
while page <= max_pages:
url = "https://www.thenewboston.com/trade/search.php?pages=" + str(page)
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
for link in soup.findAll('a', {'class' : 'item-name'}):
href = "https://www.thenewboston.com" + link.get('href')
print(href)
page += 1
creepy_crawly(1)
我用urllib做了一个网络爬虫,它可以更快,访问https页面没有问题,但有一点是它不验证服务器证书,这使它更快但更危险(容易受到mitm攻击)。 下面是该库的一个用法示例:
从一个页面抓取HTML只需要3行,很简单,不是吗?在
有关urllib的详细信息:https://docs.python.org/2/library/urllib.html
我还建议您在HTML上使用regex来获取其他链接,例如(使用re-library)可以是:
^{pr2}$相关问题 更多 >
编程相关推荐