【发布时间】:2015-11-11 23:03:44
【问题描述】:
我正在使用 scrapy 和 beautifulsoup 来抓取美国不同城市的所有酒店的列表。
当我进入一个名为“旧金山酒店”的页面时,它只包含该市 250 家酒店中的 30 家。单击“列表中的下一个 30”不会更改 url,也不会更改排序参数。 我的问题:我如何才能到达 250 家酒店的整个列表,或者选择要从哪个排名中抓取。谢谢。
到目前为止我的代码:
r = requests.get(url)
soup = BeautifulSoup(r.content,'html.parser')
headers = soup.find_all("h1",{"class":"X"})
for header in headers:
headerText = header.text
match=re.search('(.+ Hotels)',headerText)
if match:
writeHotels(soup,match.group(0))
def writeHotels(soup,location):
#create Hotels directory
hotelDir = 'Hotels/'
if not os.path.exists(hotelDir):
os.makedirs(hotelDir)
hotels = soup.find_all("a",{"class":"Y"})
name=location+'.txt'
#write hotels to file
if os.path.exists(hotelDir+name):
print 'opening file '+name+"\n"
else:
print 'creating file '+name+"\n"
file=open(hotelDir+name,'a')
for hotel in hotels:
file.write(hotel.text+"\n")
file.close()
【问题讨论】:
-
可以发网址吗?
-
这完全取决于页面本身,您需要使用 chrome develepor 工具或 firebug 之类的工具来检查正在执行的请求。
标签: python web-scraping beautifulsoup web-crawler scrapy