在Python中xpath爬图片src有问题
在xpath显示正常
Python执行就有问题,src前面有 .. 整个也不一样
尝试换种方式还是不行,直接报错
import urllib.request
from lxml import etree
def create_request(page):
if(page==1):
url = 'https://sc.chinaz.com/tupian/fengjing.html'
else:
url = 'https://sc.chinaz.com/tupian/fengjing_'+ str(page) + '.html'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.42'
}
request = urllib.request.Request(url=url,headers=headers)
return request
def get_content(request):
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
return content
def down_load(content):
tree = etree.HTML(content)
name_list = tree.xpath('//div[@class="container"]//div/img/@alt')
src_list = tree.xpath('//div[@class="container"]//div/img/@src')
for i in range(len(name_list)):
name = name_list[i]
src = src_list[i]
url = 'https:' + src
print(name, url)
# urllib.request.urlretrieve(url=url,filename='./fengjing/' + name + '.jpg')
if __name__ == '__main__':
start_page = int(input('请输入起始页码'))
end_page = int(input('请输入结束页'))
for page in range(start_page,end_page+1):
request = create_request(page)
content = get_content(request)
down_load(content)