【发布时间】:2014-12-28 19:41:18
【问题描述】:
我正在尝试制作一个可以使用 BeautifulSoup 循环浏览页面的网络爬虫
为此,我正在尝试编写一个函数来调用我正在寻找的页面,找到下一个按钮的 Href 打印结果,然后将其分配给请求并重复该函数以递归方式打印每个下一个按钮的新值。
这就是我所拥有的,但我无法真正弄清楚它不起作用。我没有收到任何错误,所以我认为我的结构可能已经关闭。
提前谢谢你。
import urllib.request
from bs4 import BeautifulSoup
import re
url = "http://www.calaiswine.co.uk/products/type/all-wines/1.aspx"
root_url = "http://www.calaiswine.co.uk"
first_index_url = '/products/type/all-wines/1.aspx'
htmlFile = urllib.request.urlopen(url);
htmlText = htmlFile.read();
soup = BeautifulSoup(htmlText);
def cycle_to_next_page(foo):
response = urllib.request.urlopen( root_url + foo)
soup = BeautifulSoup(response)
items = [a.attrs.get('href') for a in soup.findAll('a', title='Next')]
print (cycle_to_next_page(items[0]))
cycle_to_next_page(first_index_url)
【问题讨论】:
-
你的递归循环是如何终止的?
-
你想用什么语言写作?您的代码中不应有任何 ;
标签: python recursion request web-scraping beautifulsoup