import requests from lxml import etree e ittys:/hal.aLuhaodar s o/y siub.ctenuy.hpid-1748281 e÷NE46890%56968945证5685818ED78A686f1te=4%fHJTthaHlDblang-ChineseEquiokAload=-80+bpage=al b69gunl : littit/⑴al.a lacnreas conlyv i.cte n.y.1pnl1e1274821ere报548902证6430689A3短50858儿1E78468kf1.ter 4ik1.1r .l02 .np C.h1nesekqwuic.k10n3138B.6AeER.e, index = 0 for page in range(1,31): print('正在爬取第'+str(page)+'页")url = base_url+str(page) #/ /img[@class="img-responsive big-thumb thumb-desktop" ]/@srcresponse = requests.get(url=url) content = response.text # print(content) tree = etree.HTML(content) img_li = tree.xpath( ' / /img[@class="img-responsive big-thumb thumb-desktop"]/@src' )for img_url in img_li: print('正在爬取第"+str(index)+'张') img_response = requests.get(url=img_url)img_content = img_response.content index += 1 # print(img_url) extension = ' . ' +img_url.split( '. ')[-1] fp.write(img_content)
时间: 2024-04-01 17:38:22 浏览: 32
这段代码是一个Python爬虫,用于爬取一个网站上的图片。它首先导入了requests和lxml库,然后定义了一个基础的URL和页码范围。在循环中,它使用requests库获取每一页的内容,然后使用lxml库解析HTML内容并提取出所有图片的URL。接着,它再次使用requests库获取每个图片的内容,并将它们写入文件。最后,它使用图片的URL中的扩展名来为文件命名。
相关问题
使用lxml的etree爬取http://www.kanunu8.com/book3/6879/每一章节的内容
以下是一个示例代码,演示如何使用lxml的etree爬取 http://www.kanunu8.com/book3/6879/ 每一章节的内容:
```python
import requests
from lxml import etree
# 发送请求并获取HTML
url = 'http://www.kanunu8.com/book3/6879/'
response = requests.get(url)
html_string = response.content.decode('gbk')
# 将HTML字符串转换为etree对象
root = etree.HTML(html_string)
# 获取章节列表
chapter_list = root.xpath('//div[@class="list"]/ul/li/a/@href')
# 循环获取每个章节的内容
for chapter in chapter_list:
chapter_url = url + chapter
response = requests.get(chapter_url)
chapter_html_string = response.content.decode('gbk')
chapter_root = etree.HTML(chapter_html_string)
title = chapter_root.xpath('//h1/text()')[0]
content_list = chapter_root.xpath('//div[@class="main"]/text()')
content = ''.join(content_list)
print(title)
print(content)
```
在这个示例中,我们首先使用`requests`库发送请求并获取HTML。然后,我们将HTML字符串转换为etree对象,并使用XPath表达式获取章节列表。接着,我们循环遍历每个章节的链接,发送请求并获取HTML,然后将HTML字符串转换为etree对象。最后,我们使用XPath表达式获取章节标题和内容,并输出结果。需要注意的是,由于该网站使用gbk编码,我们需要在获取HTML后使用`.decode('gbk')`对其进行解码。
import requests from lxml import etree url="https://www.80142.com/txt/82776.html" html=requests.get(url) print(html)出现requests not get
这个问题可能是因为您没有正确安装 requests 库。请尝试在命令行中使用以下命令安装 requests 库:
```
pip install requests
```
如果您已经安装了 requests 库,那么可能是因为您的代码中出现了其他问题。请检查您的代码是否正确导入了 requests 库,并且 URL 是否正确。另外,您的代码中缺少换行符,请注意格式。下面是您的代码的修改版:
```
import requests
from lxml import etree
url = "https://www.80142.com/txt/82776.html"
html = requests.get(url)
print(html)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)