# 发起请求 headers = { 'User-Agent': random.choice(user_agent_list) } # print(headers) # for page in range(11,84): url = f'http://www.chinawuliu.com.cn/zcms/ui/catalog/15196/pc/index_11.shtml' # url = 'http://www.chinawuliu.com.cn/zixun/hgjj/' response = requests.get(url=url,proxies=get_proxies(),headers=headers,timeout=8) response.encoding = 'utf-8' # 解析请求 etree = html.fromstring(response.text) info_url = etree.xpath('/html/body/main/div/div[1]/div[3]/ul/li/p[1]/a/@href') t_url = [f"http://www.chinawuliu.com.cn{t_url}" for t_url in info_url] for detail_url in t_url: res = requests.get(url=detail_url, headers=headers,proxies=get_proxies(),timeout=5) res.encoding = 'utf-8' tree = html.fromstring(res.text) name = tree.xpath('/html/body/main/div/div[1]/div[1]/span/text()')这个代码为什么不输出name就结束进程了
时间: 2024-04-25 15:27:23 浏览: 18
这个代码中只有获取name的语句,而没有对name进行任何处理和输出的语句,所以即使成功获取到了name,也不会有任何输出。如果想要输出name,可以在获取到name后添加输出语句,比如:
```
name = tree.xpath('/html/body/main/div/div[1]/div[1]/span/text()')
print(name)
```
这样就可以将获取到的name输出到控制台上了。
相关问题
headers = { 'User-Agent': random.choice(user_agent_list) } # print(headers) # for page in range(11,84): url = f'http://www.chinawuliu.com.cn/zcms/ui/catalog/15196/pc/index_11.shtml' # url = 'http://www.chinawuliu.com.cn/zixun/hgjj/' response = requests.get(url=url,proxies=get_proxies(),headers=headers,timeout=8) response.encoding = 'utf-8' # 解析请求 etree = html.fromstring(response.text) info_url = etree.xpath('/html/body/main/div/div[1]/div[3]/ul/li/p[1]/a/@href') t_url = [f"http://www.chinawuliu.com.cn{t_url}" for t_url in info_url] for detail_url in t_url: res = requests.get(url=detail_url, headers=headers,proxies=get_proxies(),timeout=5) res.encoding = 'utf-8' tree = html.fromstring(res.text) name = tree.xpath('/html/body/main/div/div[1]/div[1]/span/text()') print(name)这个代码为什么不输出name就结束进程了
你的代码看起来没有什么问题,可以正常输出name。可能是因为你在运行代码时,没有获取到符合条件的数据,导致输出为空。你可以在for循环外面添加一个判断,判断是否获取到了name,如果获取到了就输出,否则提示没有获取到数据。具体来说,你可以这样修改代码:
```
headers = { 'User-Agent': random.choice(user_agent_list) }
url = f'http://www.chinawuliu.com.cn/zcms/ui/catalog/15196/pc/index_11.shtml'
response = requests.get(url=url,proxies=get_proxies(),headers=headers,timeout=8)
response.encoding = 'utf-8'
etree = html.fromstring(response.text)
info_url = etree.xpath('/html/body/main/div/div[1]/div[3]/ul/li/p[1]/a/@href')
t_url = [f"http://www.chinawuliu.com.cn{t_url}" for t_url in info_url]
for detail_url in t_url:
res = requests.get(url=detail_url, headers=headers,proxies=get_proxies(),timeout=5)
res.encoding = 'utf-8'
tree = html.fromstring(res.text)
name = tree.xpath('/html/body/main/div/div[1]/div[1]/span/text()')
if name:
print(name)
else:
print('没有获取到数据')
```
这样可以更清晰地判断是否获取到了数据。
introduction_url = "https://pvp.qq.com/web201605/herodetail/" + str(ename) + ".shtml" # 拼接英雄介绍页面地址 response = requests.get(introduction_url, headers={"user-agent": random.choice(my_headers)}).content.decode('gbk')解释代码所有部分
这段代码是一个Python程序,主要包含以下几个部分:
1. 定义一个字符串变量`introduction_url`,用于存储英雄介绍页面的网址。其中,`str(ename)`是将英雄编号转换为字符串类型,`.shtml`是网址的后缀。
2. 使用`requests`库向英雄介绍页面地址发送一个HTTP请求,获取页面内容。`headers={"user-agent": random.choice(my_headers)}`是为了设置请求头,其中`my_headers`是一个包含多个浏览器请求头的列表,`random.choice()`会随机选择一个请求头,避免被反爬虫机制识别。`.content`表示返回的是二进制数据,`.decode('gbk')`将二进制数据用gbk编码方式解码为字符串。
总体来说,这段代码的作用是根据英雄编号拼接出对应的英雄介绍页面地址,并向该地址发送HTTP请求,获取页面内容,以便后续对页面进行解析和数据提取。
相关推荐
![rpm](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)