【发布时间】:2016-10-31 11:42:06
【问题描述】:
我一直在关注 TheNewBoston 使用 Pycharm 的 Python 3.4 教程,目前正在学习如何创建网络爬虫的教程。我只想下载 XKCD 的所有漫画。使用看起来很容易的存档。这里是my code,后面是TheNewBoston。
每当我运行代码时,什么都没有发生。它运行并说,“进程以退出代码0完成”我在哪里搞砸了?
TheNewBoston 的教程有点过时,用于抓取的网站已更改域。我将评论视频中似乎很重要的部分。
我的代码:
mport requests
from urllib import request
from bs4 import BeautifulSoup
def download_img(image_url, page):
name = str(page) + ".jpg"
request.urlretrieve(image_url, name)
def xkcd_spirder(max_pages):
page = 1
while page <= max_pages:
url = r'http://xkcd.com/' + str(page)
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
for link in soup.findAll('div', {'img': 'src'}):
href = link.get('href')
print(href)
download_img(href, page)
page += 1
xkcd_spirder(5)
【问题讨论】:
-
你还没有解释发生了什么问题,或者问了一个问题......?不过,我猜你在
download_img中说name 'page' is undefined时会出错——因为page只存在于xkcd_spirder中,你不能在其他地方使用它。您需要将其作为参数传递给download_img。 -
我只是尝试了我假设你的意思。还是很新的。这是新代码。和以前一样的问题。还编辑了原始帖子以实际遇到我的问题。有点激动,哈哈。 pastebin.com/nv6X7S0M
标签: python web beautifulsoup