【发布时间】:2015-07-30 05:28:26
【问题描述】:
我正在尝试使用 Python 和 Beautiful Soup 从特定页面中提取一些信息。我发现request.get(url) 即使在我请求不同的页面时也不会更改页面。
在下面的行中,我定义了我要抓取的页面,所有返回pagenumber=1,即使我尝试使用pagenumber=2,它也从第一页开始并只抓取第一页。
activepage = soup.find('ul', id= 'pagination').li.string
print "Page Number: " + activepage
我在其他页面上测试了我的代码,它运行良好,但在这个特定页面上,我无法循环浏览不同的页面。谁能告诉我这个页面的确切问题是什么以及解决方案是什么?
import requests
import sys
from bs4 import BeautifulSoup
def trade_spider(max_pages):
page_number = 1
while page_number <= max_pages:
url = "http://munich.eventful.com/events/categories/festivals_parades#!page_number=" + str(page_number) + "&category=festivals_parades"
source_code = requests.get(url)
# just get the code, no headers or anything
plain_text = source_code.text
# BeautifulSoup objects can be sorted through easy
soup = BeautifulSoup(plain_text)
category = soup.find('li', id = 'breadcrumb-label').string
activepage = soup.find('ul', id= 'pagination').li.string
print "Page Number: " + activepage
for mylist in soup.findAll('li', {'class': 'clearfix'}):
link = mylist.find('a', {'data-ga-label': 'Event Title'})
if (link is not None):
href = link.get('href')
title = link.string # just the text, not the HTML
location = mylist.find("div", {"class": "event-meta"}).strong.string
date = mylist.find("div", {"class": "event-meta"}).span.string
print(title, category, href, date, location)
page_number += 1
trade_spider(8)
【问题讨论】:
标签: python web beautifulsoup web-crawler