自用:python爬虫——百度图片

#coding: utf-8
import os
import re
import time
import urllib.request

def getHtml(url):
    print(url)
    page=urllib.request.urlopen(url)
    html=page.read()
    return html

def getImg(html):
    reg=r'"objURL":"(.*?)"'   #正则
    # 括号表示分组,将括号的内容捕获到分组当中
    #  这个括号也就可以匹配网页中图片的url    imgre=re.compile(reg)
    html = html.decode('utf-8')
    imglist=re.findall(imgre,html)
    l=len(imglist)
    return imglist

def downLoad(urls,path):
    index = (page - 1) * 30 + 1
    for url in urls:
        time.sleep(1)
        try:
            res = urllib.request(url)
            if str(res.status_code)[0] != "200":
                continue
        except Exception as e:
            print(e)

        filename = os.path.join(path, str(index) + ".jpg")
        try:
            urllib.request.urlretrieve(url, filename)  # 直接将远程数据下载到本地。
            index += 1
        except Exception as e:
            print(e)
            continue

def url(page):
    html =getHtml("https://image.baidu.com/search/index?tn=baiduimage&ie=utf-8&word=%E8%AF%81%E4%BB%B6%E7%85%A7&rn=30&pn="+str((page-1)*30))
    return html

Savepath="G:/IMG"

page = 0;
while 1 :
    page = page+1
    downLoad(getImg(url(page)),Savepath)
    time.sleep(1)
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用VS Code编写Python爬虫程序,您可以按照以下步骤进行操作: 1. 打开VS Code编辑器并创建一个新的Python文件。 2. 导入所需的库或模块,例如requests和BeautifulSoup。 3. 使用requests库发送HTTP请求来获取网页的内容,可以使用类似于下面的代码: ``` import requests url = 'https://www.example.com' response = requests.get(url) ``` 其中,`url`是您要爬取的网页的URL地址,`response`是服务器的响应内容。 4. 使用BeautifulSoup库解析网页的内容,可以使用类似于下面的代码: ``` from bs4 import BeautifulSoup soup = BeautifulSoup(response.text, 'html.parser') ``` 其中,`response.text`是上一步获取的网页内容,`soup`是解析后的BeautifulSoup对象。 5. 使用BeautifulSoup对象提取所需的数据,可以使用CSS选择器或XPath表达式来定位元素。例如,如果您想提取所有具有特定类名的元素,可以使用类似于下面的代码: ``` elements = soup.select('.classname') ``` 其中,`.classname`是您要提取的元素的类名。 6. 将提取的数据保存到文件中,可以使用类似于下面的代码: ``` with open('output.txt', 'w', encoding='utf-8') as f: for element in elements: f.write(element.text + '\n') ``` 其中,`output.txt`是保存数据的文件名,`element.text`是要保存的数据。 请注意,以上代码只是一个示例,您可以根据实际情况修改和扩展代码。另外,还可以使用其他库或工具来辅助编写和运行Python爬虫程序,例如使用Scrapy框架来构建更复杂的爬虫。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python爬虫(vscode版,借鉴于知乎用户:干饭小熊猫,仅用于自用)](https://blog.csdn.net/qq_63075864/article/details/130465443)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值