希望把已经用正则表达式获取的 url 变量里的数据再次用正则表达式获取,卡在了再次获取上,请求帮助。

我想爬取wallpaper的壁纸,wallpaper的组成是一张大网页内的图片点进去是单个的大图。
我想要获取清楚一点的大图,我的正则表达式在获得小图时成功,但是进一步在原来获取的链接的基础上再次获得大图就出现了报错
requests.exceptions.MissingSchema: Invalid URL ‘urls’: No schema supplied. Perhaps you meant urls?
在查阅资料后无解,请求大神指点。:sob:

import requests
import re

header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36 Edg/81.0.416.64"
}

response = requests.get('https://wallhaven.cc/search?q=id:5&ref=fp',headers=header)

html = response.text

urls = re.findall('<a class="preview" href="(.*?)"  target="_blank"  >',html)
response1 = requests.get('urls',headers=header)

print(urls)

urls = response1.text

urls1 = re.findall('<img id="wallpaper" src="(.*?)" alt=".*?" data-wallpaper-id="eozq2o" data-wallpaper-width="1253" data-wallpaper-height="900" crossOrigin="anonymous" />',urls)

print(urls1)

#报错内容:requests.exceptions.MissingSchema: Invalid URL 'urls': No schema supplied. Perhaps you meant http://urls?
最佳答案

完整的爬取代码仅供参考:

import requests
from lxml import etree

header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36 Edg/81.0.416.64"
}

text = requests.get('https://wallhaven.cc/search?q=id:5&ref=fp',headers=header).text
html = etree.HTML(text)
img_urls = html.xpath('//img[@alt="loading"]/@data-src')
for url in img_urls:
    image = requests.get(url, headers=header).content
    img_name = url.split('/')[5]
    with open('D:/图片/%s'%img_name,'wb') as f:
        f.write(image)

注意:

  1. 请在d盘目录下创建一个叫图片的文件夹
  2. 在终端输入pip install lxml来下载xpath模块(xpath在爬虫中很有用的,不推荐正则爬虫)
3年前 评论
weishenmene (楼主) 3年前
Coolest (作者) 3年前
Coolest (作者) 3年前
Coolest (作者) 3年前
weishenmene (楼主) 3年前
weishenmene (楼主) 3年前
Coolest (作者) 3年前
Coolest (作者) 3年前
讨论数量: 2
Jason990420
  1. 网址不是 'urls' 字符串, 而且就算用变量 urls 也是错的, 因为 urls 是网址字符串的列表.
response1 = requests.get('urls',headers=header)  
  1. 这些参数对每一个网址来说都是不一样的, 如果这样指定, 会找不到匹配的
urls1 = re.findall('<img id="wallpaper" src="(.*?)" alt=".*?" data-wallpaper-id="eozq2o" data-wallpaper-width="1253" data-wallpaper-height="900" crossOrigin="anonymous" />',urls)
  1. 修改后的代码
import requests
import re

header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36 Edg/81.0.416.64"
}

response = requests.get('https://wallhaven.cc/search?q=id:5&ref=fp',headers=header)

html = response.text

urls = re.findall('<a class="preview" href="(.*?)"  target="_blank"  >',html)
for url in urls:
    response1 = requests.get(url, headers=header)
    print(url)
    text = response1.text
    urls1 = re.findall('<img id="wallpaper" src="(.*?)" alt=".*?" data-wallpaper-id=".*?" data-wallpaper-width=".*?" data-wallpaper-height=".*?" crossOrigin="anonymous" />',text)
    print(urls1)
3年前 评论
weishenmene (楼主) 3年前

完整的爬取代码仅供参考:

import requests
from lxml import etree

header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36 Edg/81.0.416.64"
}

text = requests.get('https://wallhaven.cc/search?q=id:5&ref=fp',headers=header).text
html = etree.HTML(text)
img_urls = html.xpath('//img[@alt="loading"]/@data-src')
for url in img_urls:
    image = requests.get(url, headers=header).content
    img_name = url.split('/')[5]
    with open('D:/图片/%s'%img_name,'wb') as f:
        f.write(image)

注意:

  1. 请在d盘目录下创建一个叫图片的文件夹
  2. 在终端输入pip install lxml来下载xpath模块(xpath在爬虫中很有用的,不推荐正则爬虫)
3年前 评论
weishenmene (楼主) 3年前
Coolest (作者) 3年前
Coolest (作者) 3年前
Coolest (作者) 3年前
weishenmene (楼主) 3年前
weishenmene (楼主) 3年前
Coolest (作者) 3年前
Coolest (作者) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!