如何在不变的URL中抓取不同城市的多个页面 - Python 3

问题描述 投票:1回答:3

我正在访问不同的网站来练习网页抓取并尝试网络抓取以下网站 - http://www.pizzahut.com.cn/StoreList

我之前在网站上发布了一个类似的问题,抓住了同一个网站。提供的答案非常好,允许我提取一个城市中所有商店的所有lat和long。但是,我还想从多个城市提取lat和long来存储并遇到一个我需要一些指导的问题。更改城市也不会改变URL。

该网站全都是中文,因此我不得不使用谷歌翻译将其翻译成英文并一直工作。

enter image description here

我如何选择不同的城市显示在上面突出显示为红色的图像中。我基本上点击了那个链接并选择了我要显示的城市。我想看看商店在不同城市的经纬度,或者只是北京的例子。

下面是我目前正在使用的代码。以下代码仅提取上海所有商店的纬度和经度,该商店显示在默认页面上。

import os
import requests
import csv
import time
from bs4 import BeautifulSoup

csv_name = 'Lat_long_practice.csv'
csv = open(csv_name, 'w', encoding='utf-8-sig')

columnTitleRow = "Name, Latitude, Longitude\n"
csv.write(columnTitleRow)

for page_no in range(1, 14):
    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 #Get the name of the store
        info = row.find('input').get('value')
        location = info.split('|')

        #print(location)
        row = ''

        if location[0] == '':
            print(name)
            row = name + ',' + '' + ',' + '' + '\n'
            csv.write(row)
        else:
          location_data = location[0].split(',')
            latitude = location_data[0]
            longitude = location_data[1]
            print(name, latitude, longitude)
            row = name + ',' + latitude + ',' + longitude + '\n'
            csv.write(row)

感谢您阅读一个很长的问题。如果有人能弄明白我如何使用python和beautifulsoup来提取不同城市的所有坐标并使用不变的URL,那真是太棒了。

python html web-scraping beautifulsoup
3个回答
1
投票

当你点击一个城市时,会调用addCookie函数,这是按照格式iplocation={city}|0|0将城市存储在cookie中,这样你就可以通过发送适当的cookie来获得某个城市,例如:

from urllib.parse import quote_plus
page = requests.post('http://www.pizzahut.com.cn/StoreList/Index', data=data, cookies={'iplocation': quote_plus('北京市|0|0')})

1
投票

https://www.tripadvisor.com/也有静态网址。

你可以用seleniumphantomjs来解决这个问题

from selenium import webdriver
url1 = "https://www.tripadvisor.com.tr/Restaurants-g293974-Istanbul.html"

executable_path1 = './phantomjs'
driver = webdriver.PhantomJS(executable_path=executable_path1)
driver.get(url1)

#selecting links in the page
#doing another things

nextpage = driver.find_element_by_xpath("//*[@id='EATERY_LIST_CONTENTS']/div[3]/div//a[contains(.,'Sonraki')]")
nextpage.click()

在伊斯坦布尔,有386页与餐馆有关,所有页面的网址都是相同的(又名。不变的网址,静态网址)

这是我擦除tripadvisor的代码之一。我希望它可以帮助你

有关更多信息,请查看http://selenium-python.readthedocs.io/navigating.html


0
投票

如果我理解正确,这里的实际问题是维护和迭代所有可用的城市,一次一个。

  1. 单击“更改城市”按钮(xpath://a[contains(@class,'chose_city')])。
  2. 幸运的是,列表一次性加载,因此我们可以即时访问所有城市,而无需滚动。 所有的城市都在第二个//div[contains(@class,'city_window')],所以我们需要迭代它的孩子DIVs。
  3. 我们将使用字母索引(在A-Z中)和字母内的城市索引来维护我们的索引。所以让我们定义letterIndexcityIndex
  4. 因为有两个city_sel_box DIV,我们只会让事情变得更简单,注意第一个内部DIV相关的是30,ID chose_a2。 因此,我们可以设置letterIndex = 31并从cityIndex = 1开始。
  5. 第一个循环:迭代(//div[contains(@class,'city_window')]/div)[letterIndex]letterIndex从31到52。
  6. 内部循环:迭代(//div[contains(@class,'city_window')]/div)[letterIndex]/div/a[cityIndex]cityIndex从1到数组(//div[contains(@class,'city_window')]/div)[31]/div/a的长度。
  7. 对于每次迭代,运行原始算法。

让我知道它是如何工作的,更重要的是 - 如果你还有其他问题。

© www.soinside.com 2019 - 2024. All rights reserved.