获取总页数 url1 = 'https://jn.lianjia.com/zufang/pg1/#contentList' response = requests.get(url1, headers=header) html = response.text match = re.search(r'data-totalpage="(\d+)"', html) if match: total_page = int(match.group(1)) else: print('未匹配到结果') # 循环爬取每一页的租房信息 for num in range(1, total_page + 1):NameError: name 'total_page' is not defined
时间: 2024-04-01 07:34:26 浏览: 21
根据你提供的代码看起来,你已经成功地从第一页的 HTML 页面中匹配到了总页数。但是,你需要将匹配到的 total_page 变量定义在之前,以确保它在后面的循环中可以正确地使用。可以像下面这样修改代码:
```
import requests
import re
url1 = 'https://jn.lianjia.com/zufang/pg1/#contentList'
header = {'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(url1, headers=header)
html = response.text
match = re.search(r'data-totalpage="(\d+)"', html)
if match:
total_page = int(match.group(1))
# 循环爬取每一页的租房信息
for num in range(1, total_page + 1):
# 在这里可以编写爬取每一页租房信息的代码
else:
print('未匹配到结果')
```
这样就可以避免 `NameError: name 'total_page' is not defined` 的错误了。
相关问题
获取总页数 url1 = https://jn.lianjia.com/zufang/pg1/#contentList response = requests.get(url1, headers=header) html = response.text match = re.search(r data-totalpage=(\d+) , html) if match: total_p
age = int(match.group(1)),这是一个获取链家网租房信息总页数的 Python 代码段。具体来说,使用 requests 库发送 GET 请求获取链家网租房首页(pg1)的 HTML 内容,然后使用正则表达式从 HTML 内容中匹配 data-totalpage 属性的值,即租房信息的总页数,并将其转换为整数类型赋值给变量 total_page。注意,这里使用了伪装头(headers)来模拟浏览器访问,以避免被反爬虫机制拦截。
url1 = 'https://jn.lianjia.com/zufang/pg1/#contentList' response = requests.get(url1, headers=header) html = response.text match = re.search(r'data-totalpage="(\d+)"', html) if match: total_page = int(match.group(1)) else: print('未匹配到结果')for num in range(1, total_page + 1):中没找到total_page
你需要将获取总页数的代码放在循环之前,确保能够成功获取总页数,然后再进行循环。
以下是一个示例代码:
```python
import requests
import re
url = 'https://jn.lianjia.com/zufang/pg1/#contentList'
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.36 Edge/16.16299'
}
# 获取总页数
response = requests.get(url, headers=headers)
html = response.text
match = re.search(r'data-totalpage="(\d+)"', html)
if match:
total_page = int(match.group(1))
print(total_page)
else:
print('未匹配到结果')
# 循环获取每一页数据
for num in range(1, total_page + 1):
url = f'https://jn.lianjia.com/zufang/pg{num}/#contentList'
response = requests.get(url, headers=headers)
html = response.text
# 提取数据的代码
# ...
```
这个示例代码先获取总页数,然后再进行循环,获取每一页的数据。你需要根据自己的需求,修改提取数据的代码。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)