网络爬虫学习,print(f“正在下载{pic_name}...“),SyntaxError: invalid syntax 错误原因及解决方法

目录:
一。解决问题
二。解决方法
三。原始代码
四。修改后的代码

一。解决问题
学习爬虫,想直接用网上的code,结果报错如下。

print(f"正在下载{pic_name}...")
                               ^

SyntaxError: invalid syntax
原文链接:https://blog.csdn.net/qq_40169189/article/details/107786383
查找网络,原来是python 版本问题;多次搜索,凡是vmgirl网站相关的大多数版本都是python3.5/python3.6, 用python2.7.14 去用太吃力了。

二。解决方法
修改了代码,改到python2.7支持的格式:
python2.7.14, print(“正在下载{}”).format(pic_name)
python3.6, print(f"正在下载{pic_name}…")
python2.7, print(“所有小姐姐图片下载完毕!保存在{}下”).format(os.getcwd())
python3.6, print(f"所有小姐姐图片下载完毕!保存在{os.getcwd()}\下")
错误消失了,但是代码仍然不能抓到有用的数据。

三。原始代码
https://blog.csdn.net/qq_40169189/article/details/107786383

import requests, time, re, os
from bs4 import BeautifulSoup

findpics = re.compile(r'<a href="(.*?)" alt=".*?" title=".*?">')
headers = {
    "user-agent": "Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 84.0.4147.89Safari / 537.36Edg / 84.0.522.40"
}


def main():
    url = "https://www.vmgirls.com/14236.html"
    html = askUrl(url)
    bs = BeautifulSoup(html, "html.parser")
    # 获取标题 用于创建文件夹存放图片
    titile = bs.find("h1").text
    if not os.path.isdir(titile):
        os.mkdir(titile)

    # 解析获取图片地址
    pics = re.findall(findpics, html)  # 取到的地址为image/2020/07/2020072112422623-scaled.jpeg
    # 需要自己拼接
    for pic in pics:
        # 图片连接
        pic = 'https://www.vmgirls.com/' + pic
        # 获取图片名称
        pic_name = pic.split('/')[-1]
        # print(pic_name,":",pic)
        # 保存到本地文件夹
        response = requests.get(pic, headers=headers)
        f = open(titile + "\\" + pic_name, 'wb')  # 二进制写入
        f.write(response.content)
        print(f"正在下载{pic_name}...")
    print(f"所有小姐姐图片下载完毕!保存在{os.getcwd()}\下")


# 访问url链接
def askUrl(url):
    time.sleep(1)  # 延迟一秒
    response = requests.get(url, headers=headers)
    html = response.content.decode("utf-8")
    return html


if __name__ == '__main__':
    main()


四。修改后的代码,不报错,但是还抓不到内容。请大侠指导。或者python2.7.14就是不能这样使用。

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# author:StevenC     #86189
# datetime:2021/5/4 21:19
# software: PyCharm
import sys

reload(sys)
sys.setdefaultencoding("utf-8")
import requests, time, re, os
from bs4 import BeautifulSoup

findpics = re.compile(r'<a href="(.*?)" alt=".*?" title=".*?">')
headers = {
    "user-agent": "Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 84.0.4147.89Safari / 537.36Edg / 84.0.522.40"
}


def main():
    url = "https://www.vmgirls.com/14236.html"
    html = askUrl(url)
    bs = BeautifulSoup(html, "html.parser")
    # 获取标题 用于创建文件夹存放图片
    titile = bs.find("h1").text
    if not os.path.isdir(titile):
        os.mkdir(titile)

    # 解析获取图片地址
    pics = re.findall(findpics, html)  # 取到的地址为image/2020/07/2020072112422623-scaled.jpeg
    # 需要自己拼接
    for pic in pics:
        # 图片连接
        pic = 'https://www.vmgirls.com/' + pic
        # 获取图片名称
        pic_name = pic.split('/')[-1]
        # print(pic_name,":",pic)
        # 保存到本地文件夹
        response = requests.get(pic, headers=headers)
        f = open(titile + "\\" + pic_name, 'wb')  # 二进制写入
        f.write(response.content)
        print("正在下载{}").format(pic_name)        #python2.7 format
        #print(f"正在下载{pic_name}...")  #python3.6 format.
    print("所有小姐姐图片下载完毕!保存在{}下").format(os.getcwd())     #python2.7 format
    #print(f"所有小姐姐图片下载完毕!保存在{os.getcwd()}\下")


# 访问url链接
def askUrl(url):
    time.sleep(1)  # 延迟一秒
    response = requests.get(url, headers=headers)
    html = response.content.decode("utf-8")
    return html


if __name__ == '__main__':
    main()

"""
https://blog.csdn.net/qq_40169189/article/details/107786383
    print(f"正在下载{pic_name}...")
                                   ^
SyntaxError: invalid syntax
"""
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值