帮我用python写一个爬取百度迁徙全国热门城市迁入迁出数据的代码
时间: 2023-09-23 16:10:15 浏览: 52
好的,以下是Python爬取百度迁徙全国热门城市迁入迁出数据的代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://huiyan.baidu.com/migration/cityrank.jsonp?dt=city&id=420100&type=move_out&date=20210101&callback=jsonp_1627648939140_83894'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
response = requests.get(url, headers=headers)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
print(soup.prettify())
```
其中,我们通过requests库向目标URL发送GET请求,获取到返回的HTML代码,然后使用BeautifulSoup库解析HTML代码,最后打印出来。这里的URL是获取武汉市(id=420100)迁出数据的URL,你可以根据需要修改URL中的参数来获取其他城市的数据。
请注意,以上代码仅供学习参考,如需商用请遵守相关法律法规。
相关问题
帮我写一个python爬取百度地图全国迁出迁入数据
当然,我可以帮您解决这个问题。您可以使用Python中的requests库和BeautifulSoup库来爬取百度地图的迁徙数据。这里有一个示例代码,可以供您参考:
```python
import requests
from bs4 import BeautifulSoup
url = "https://huiyan.baidu.com/migration/cityrank.jsonp?dt=city&id=1&type=move_out&date=20210101&callback=jsonp_1610058490762_32804"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
data = soup.text.strip()[19:-1]
print(data)
```
这段代码可以获取2021年1月1日的全国迁出数据。您可以根据需要修改日期和数据类型。希望能对您有所帮助!
帮我用python写一个爬取全球国家城市信息的代码
你好,我可以回答这个问题。以下是一个简单的 Python 爬虫代码,可以爬取全球国家城市信息:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.worldometers.info/geography/alphabetical-list-of-countries/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', {'class': 'table table-hover'})
rows = table.find_all('tr')
for row in rows:
cells = row.find_all('td')
if len(cells) > 1:
country = cells[0].text.strip()
city = cells[1].text.strip()
print(country, city)
```
这个代码使用 requests 库发送 HTTP 请求,然后使用 BeautifulSoup 库解析 HTML 页面。它会爬取 https://www.worldometers.info/geography/alphabetical-list-of-countries/ 这个页面上的表格,然后输出每个国家和城市的信息。