Dan*_*Lee 7 javascript python selenium beautifulsoup web-scraping
我最近接触了网页抓取并尝试抓取各种网页。目前,我正在尝试抓取以下站点 - http://www.pizzahut.com.cn/StoreList
到目前为止,我已经使用硒来刮取经度和纬度。但是,我的代码现在只提取第一页。我知道有一个动态网页抓取可以执行 javascript 并加载不同的页面,但是很难找到正确的解决方案。我想知道是否有办法访问其他 49 页左右,因为当我单击下一页时,URL 不会更改,因为它已设置,所以我不能每次都迭代不同的 URL
以下是我到目前为止的代码:
import os
import requests
import csv
import sys
import time
from bs4 import BeautifulSoup
page = requests.get('http://www.pizzahut.com.cn/StoreList')
soup = BeautifulSoup(page.text, 'html.parser')
for row in soup.find_all('div',class_='re_RNew'):
name = row.find('p',class_='re_NameNew').string
info = row.find('input').get('value')
location = info.split('|')
location_data = location[0].split(',')
longitude = location_data[0]
latitude = location_data[1]
print(longitude, latitude)
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助。非常感激
在浏览器中打开开发者工具(对于 Google Chrome,它是Ctrl+ Shift+ I)。现在,转到XHR位于Network选项卡内的选项卡。
完成后,单击下一页按钮。您将看到以下文件。
单击该文件。在General块中,您将看到我们需要的这两个东西。
向下滚动,在“表单数据”选项卡中,您可以看到 3 个变量为
在这里,您可以看到更改 的值pageIndex将提供所需的所有页面。
现在,我们已经获得了所有需要的数据,我们可以使用上述数据POST为 URL编写一个方法http://www.pizzahut.com.cn/StoreList/Index。
我将向您展示抓取前 2 页的代码,您可以通过更改range().
for page_no in range(1, 3):
data = {
'pageIndex': page_no,
'pageSize': 10,
'keyword': '???????????'
}
page = requests.post('http://www.pizzahut.com.cn/StoreList/Index', data=data)
soup = BeautifulSoup(page.text, 'html.parser')
print('PAGE', page_no)
for row in soup.find_all('div',class_='re_RNew'):
name = row.find('p',class_='re_NameNew').string
info = row.find('input').get('value')
location = info.split('|')
location_data = location[0].split(',')
longitude = location_data[0]
latitude = location_data[1]
print(longitude, latitude)
Run Code Online (Sandbox Code Playgroud)
输出:
PAGE 1
31.085877 121.399176
31.271117 121.587577
31.098122 121.413396
31.331458 121.440183
31.094581 121.503654
31.270737000 121.481178000
31.138214 121.386943
30.915685 121.482079
31.279029 121.529255
31.168283 121.283322
PAGE 2
31.388674 121.35918
31.231706 121.472644
31.094857 121.219961
31.228564 121.516609
31.235717 121.478692
31.288498 121.521882
31.155139 121.428885
31.235249 121.474639
30.728829 121.341429
31.260372 121.343066
Run Code Online (Sandbox Code Playgroud)
注意:您可以通过更改值pageSize(当前为 10)来更改每页的结果。
| 归档时间: |
|
| 查看次数: |
4937 次 |
| 最近记录: |