zm_selina 发表于 2019-12-30 21:25:30

我的第一个爬虫

废话少说,先上图:

    在网上下载gho系统时,找到了一个叫技术员联盟的网站(http://www.jsgho.net),有个图片的标签,点开一看,哇,全是美女(穿了衣服的),刚好学到爬虫,练练手吧。观察了一下,发现所有的图片都在www.jsgho.net/image/下面,查看了一上网页的源代码,图片在<li></li>标签的<img src里面,不过有个小问题就是网址不全,取下来后还要加上网站的地址,开工。(附全部网址),新人,代码写的很烂,请大神们多多指点,谢谢。
import requests
from bs4 import BeautifulSoup
import re
import os


def getHTMLText(url):
    try:
      headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
                                 '(KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'}
      r = requests.get(url, headers=headers)
      r.raise_for_status()
      r.encoding = 'utf-8'
      return r.text
    except:
      return ''

def get_img(html):
    try:
      p = r'<li><img src="([^"]*\.jpg)"'
      imglist = re.findall(p, html)
      soup = BeautifulSoup(html, 'lxml')
      name = soup.title.text
      root = r"D:\Pictures//"
      path = root + name
    except:
      print("")

    try:
      os.mkdir(path)
    except FileExistsError:
      pass

    os.chdir(path)

    for each in imglist:
      filename = each.split('/')[-1]
      url = "http://www.jsgho.net"
      each = url + each
      r = requests.get(each)

      with open(filename, 'wb') as f:
            f.write(r.content)

def main(url):
    html = getHTMLText(url)
    get_img(html)


with open("C://Users//Administrator//Desktop//sitemap.txt") as f:
    urls = f.readlines()

for url in urls:
    url = url.strip('\n')
    main(url)

czfasaoyou 发表于 2019-12-31 13:34:52

Traceback (most recent call last):
File "D:\python\test.py", line 2, in <module>
    from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'

请问这个错误是哪错了

zm_selina 发表于 2019-12-31 14:55:42

czfasaoyou 发表于 2019-12-31 13:34
Traceback (most recent call last):
File "D:\python\test.py", line 2, in
    from bs4 import Bea ...

没有 bs4 模块
需要安装,在命令行下运行:pip install bs4

czfasaoyou 发表于 2019-12-31 16:27:51

谢谢

czfasaoyou 发表于 2019-12-31 16:30:40

UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 11: illegal multibyte sequence,这个又是哪里错了?谢谢

zm_selina 发表于 2019-12-31 20:58:15

czfasaoyou 发表于 2019-12-31 16:30
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 11: illegal multibyte sequence, ...

编码错误,把完整的异常提示发来看看。

czfasaoyou 发表于 2020-1-2 16:49:52

Traceback (most recent call last):
File "D:\python\test.py", line 52, in <module>
    urls = f.readlines()
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 11: illegal multibyte sequence

zm_selina 发表于 2020-1-2 21:03:24

czfasaoyou 发表于 2020-1-2 16:49
Traceback (most recent call last):
File "D:\python\test.py", line 52, in
    urls = f.readlines ...

把这句with open("C://Users//Administrator//Desktop//sitemap.txt") as f:
改成with open("C://Users//Administarator//Desktop//sitemap.txt", encoding='utf-8') as f:看看

还有你要看看你下载的sitemap.txt文件的路径,把C://Users//Administarator//Desktop替换成你的sitemap.txt的路径。

czfasaoyou 发表于 2020-1-4 14:05:11

谢谢,我试试

czfasaoyou 发表于 2020-1-4 14:16:20

修改,运行的时候提出错误syntaxerrorinvalid character in identifier ,光标停在encoding

zm_selina 发表于 2020-1-4 16:22:58

czfasaoyou 发表于 2020-1-4 14:16
修改,运行的时候提出错误syntaxerrorinvalid character in identifier ,光标停在encoding

直接复制的吗?看看有没有中文的空格。

czfasaoyou 发表于 2020-1-6 16:01:46

直接复制的
页: [1]
查看完整版本: 我的第一个爬虫