python爬虫 将在线html网页中的图片链接替换成本地链接并将html文件下载到本地 Python写爬虫,得到了图片链接,怎样写入文件

python\u722c\u866b\uff0c\u6293\u53d6\u4e00\u4e2a\u9875\u9762\u4e2d\u6240\u6709\u94fe\u63a5\u5185\u7684\u6587\u5b57\u548c\u56fe\u7247\u5e76\u4fdd\u5b58\u5728\u672c\u5730\u600e\u4e48

\u5e76\u4e0d\u662f\u6240\u6709\u7684\u7f51\u7ad9\u7ed3\u6784\u90fd\u662f\u4e00\u6837\u7684\uff0c\u4f60\u8bf4\u7684\u529f\u80fd\u5927\u4f53\u53ef\u4ee5\u7528Python\u5b9e\u73b0\uff0c\u4f46\u5e76\u6ca1\u6709\u5199\u597d\u7684\u901a\u7528\u4ee3\u7801\uff0c\u8fd8\u9700\u8981\u6839\u636e\u4e0d\u540c\u7684\u7f51\u9875\u53bb\u505a\u8c03\u8bd5\u3002

img_req=urllib2.Request(Img_URL)img_response=opener.open(img_req)try: out=open('code','wb') #print img_response.read() out.write(img_response.read()) out.flush() out.close() print 'get code success'except IOError: print 'file wrong'

import os,re
def check_flag(flag):
regex = re.compile(r'images\/')
result = True if regex.match(flag) else False
return result

#soup = BeautifulSoup(open('index.html'))
from bs4 import BeautifulSoup
html_content = '''
<a href="https://xxx.com">测试01</a>
<a href="https://yyy.com/123">测试02</a>
<a href="https://xxx.com">测试01</a>
<a href="https://xxx.com">测试01</a>
'''
file = open(r'favour-en.html','r',encoding="UTF-8")
soup = BeautifulSoup(file, 'html.parser')
for element in soup.find_all('img'):
if 'src' in element.attrs:
print(element.attrs['src'])
if check_flag(element.attrs['src']):
#if element.attrs['src'].find("png"):
element.attrs['src'] = "michenxxxxxxxxxxxx" +'/'+ element.attrs['src']

print("##################################")
with open('index.html', 'w',encoding="UTF-8") as fp:
fp.write(soup.prettify()) # prettify()的作⽤是将sp美化⼀下,有可读性

这段Python代码的作用是对网页HTML文件进行解析,并对其中的img标签中的src属性进行修改。具体来说,该代码使用了Python内置的os和re库,以及第三方库BeautifulSoup。
首先,该代码通过打开一个名为'favour-en.html'的HTML文件,使用BeautifulSoup库对其进行解析,将其转化为一个BeautifulSoup对象,存储在变量soup中。然后,该代码遍历soup对象中的所有img标签,并检查其中是否包含'src'属性。
接下来,代码调用了check_flag函数,该函数使用正则表达式判断'src'属性中是否包含字符串'images/',如果包含则返回True,否则返回False。如果check_flag函数返回True,则修改该img标签的'src'属性,在其前面添加字符串'michenxxxxxxxxxxxx'和'/',以此对'src'属性进行修改。修改后的结果被打印出来,并将最终的HTML代码写入到名为'index.html'的文件中。
需要注意的是,在这段代码中,文件读取和写入时指定了编码方式为UTF-8,以确保能够正确读写中文字符。

import os,re
def check_flag(flag):
regex = re.compile(r'images\/')
result = True if regex.search(flag) else False
return result

#soup = BeautifulSoup(open('index.html'))
from bs4 import BeautifulSoup
html_content = '''
<a href="https://xxx.com">测试01</a>
<a href="https://yyy.com/123">测试02</a>
<a href="https://xxx.com">测试01</a>
<a href="https://xxx.com">测试01</a>
'''
file = open(r'E:\test\favour-fr.html','r',encoding="UTF-8")
soup = BeautifulSoup(file, 'html.parser')
for element in soup.find_all('img'):
if 'src' in element.attrs:
print(element.attrs['src'])
if check_flag(element.attrs['src']):
#if element.attrs['src'].find("png"):
element.attrs['src'] = "michenxxxxxxxxxxxx" +'/'+ element.attrs['src']

print("##################################")
with open('index.html', 'w',encoding="UTF-8") as fp:
fp.write(soup.prettify()) # prettify()的作⽤是将sp美化⼀下,有可读性

正则匹配原链接替换为本地路径即可

扩展阅读:www.sony.com.cn ... 免费爬虫网站 ... python在线编程入口 ... java python ... python网站 ... 在线提取youtube视频 ... python爬虫app下载 ... python爬虫完整代码 ... 爬虫手机版 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网