python爬虫爬取网页图片无法显示问题,不知道是不是有反爬虫还是代码有问题,代码在回答栏
你题目的解答代码如下:
#coding=gbk
import requests
from io import BytesIO
from PIL import Image
from selenium import webdriver
import lxml.html
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.add_experimental_option('excludeSwitches', ['enable-automation'])
opt.add_argument('--disable-blink-features')
opt.add_argument('--disable-extensions')
opt.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(options=opt)
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.huya.com/g/lol')#虎牙官网
html=driver.page_source
xp=lxml.html.fromstring(html)
u=xp.xpath('//span[@class="avatar fl"]')
for i in u:
name=i.xpath('.//img/@alt')
url=i.xpath('.//img/@data-original') #改下
print(url)
name=name[0]
url= url[0]
if url[:6]=='https:':
url=url
else:
url = 'https:'+url
print(name,url)
response = requests.get(url)
image = Image.open(BytesIO(response.content))
image.save(f'd:\w\{name}.png')
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
#coding=gbk
import requests
from io import BytesIO
from PIL import Image
from selenium import webdriver
import lxml.html
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.add_experimental_option('excludeSwitches', ['enable-automation'])
opt.add_argument('--disable-blink-features')
opt.add_argument('--disable-extensions')
opt.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(options=opt)
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.huya.com/g/lol')#虎牙官网
html=driver.page_source
xp=lxml.html.fromstring(html)
u=xp.xpath('//span[@class="avatar fl"]')
for i in u:
name=i.xpath('.//img/@alt')
url=i.xpath('.//img/@src')
print(url)
name=name[0]
url= url[0]
if url[:6]=='https:':
url=url
else:
url = 'https:'+url
print(name,url)
response = requests.get(url)
image = Image.open(BytesIO(response.content))
image.save(f'd:\w\{name}.png')
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!我主页有爬取图片相关代码