网页源代码模板如下: <html> <meta http-equiv="Content=-Type" content="text/html; charset=utf-8"/> <head> <link href="http://192.167.1.101/hint.css" rel="stylesheet"></link> </head> <body>[talkid:137031381]2014年4月20日 03:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我们已经是dffwerwer天吧! <p> ()</p> [talkid:137031382]2014年4月22日 04:45:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 音频 :[<a href="files/24434sfsfsjdfrhf_n.m4a"]>音频</a> <p> ()</p> [talkid:137031383]2014年4月23日 04:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 图片 :[<a href="files/f/f123ftrtrhf_n.jpg"]>图片</a> <p> ()</p> [talkid:137031384]2014年4月24日 05:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我们已方式方法方式 <p> ()</p> [talkid:137031385]2014年4月25日 06:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我而对方是否的天吧! <p> ()</p> </body> </html> 利用python爬虫,提取源代码字符串,并从每行body中爬取[<a href=后的链接地址,如果没有则将该行链接地址设置为NONE
时间: 2023-08-14 22:01:42 浏览: 58
您可以使用Python的正则表达式模块re来提取源代码字符串并从每行body中爬取链接地址。下面是一个示例代码,演示如何使用Python爬虫提取源代码字符串并从每行body中爬取链接地址:
```python
import re
html = '''
<html>
<meta http-equiv="Content=-Type" content="text/html; charset=utf-8"/>
<head>
<link href="http://192.167.1.101/hint.css" rel="stylesheet"></link>
</head>
<body>[talkid:137031381]2014年4月20日 03:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我们已经是dffwerwer天吧!
<p>
()</p>
[talkid:137031382]2014年4月22日 04:45:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 音频 :[<a href="files/24434sfsfsjdfrhf_n.m4a"]>音频</a>
<p>
()</p>
[talkid:137031383]2014年4月23日 04:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 图片 :[<a href="files/f/f123ftrtrhf_n.jpg"]>图片</a>
<p>
()</p>
[talkid:137031384]2014年4月24日 05:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我们已方式方法方式
<p>
()</p>
[talkid:137031385]2014年4月25日 06:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我而对方是否的天吧!
<p>
()</p>
</body>
</html>
'''
# 提取链接地址的正则表达式
link_regex = r'<a href="([^"]+)">'
# 提取源代码字符串
source_code = re.findall(r'<body>(.*?)</body>', html, re.DOTALL)[0]
# 从每行body中爬取链接地址
links = re.findall(link_regex, source_code)
links = [link if link.startswith('http') else 'NONE' for link in links]
print(links)
```
运行以上代码,将会输出链接地址的列表,如果没有链接地址,则将该行链接地址设置为"NONE"。您可以根据需要进行进一步的处理或保存。希望能对您有所帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
网页源代码模板如下: <html> <meta http-equiv="Content=-Type" content="text/html; charset=utf-8"/> <head> </head> <body>[talkid:137031381]2014年4月20日 03:55:45 , 2323234 在群 20011 中发言 我们已经是dffwerwer天吧! [talkid:137031382]2014年4月22日 04:45:45 , 2323234 在群 20011 中发言 音频 :[音频 [talkid:137031383]2014年4月23日 04:55:45 , 2323234 在群 20011 中发言 图片 :[图片 [talkid:137031384]2014年4月24日 05:55:45 , 2323234 在群 20011 中发言 我们已方式方法方式 [talkid:137031385]2014年4月25日 06:55:45 , 2323234 在群 20011 中发言 我而对方是否的天吧! </body> </html> 使用python爬虫提取body每行 中发言或 发送后的字段,包括a标签内的地址
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
网页内源代码模板如下: <html> <meta http-equiv="Content=-Type" content="text/html; charset=utf-8"> <head> </head> <body>[talkid:138031370]2014年4月20日 03:55:45 , 111222 向 232323 发送 我们已经是好友了,开始聊天吧! () [talkid:138031371]2014年4月20日 04:45:45 , 111222 向 123456 发送 音频 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.m4a"]>音频
() [talkid:138031372]2014年4月20日 04:55:45 , 111222 向 123456 发送 图片 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.jpg"]>图片 () </body> </html> 利用python爬虫,打开C:/Users/test/Desktop/DIDItest文件夹下多个文件夹内的html文件源代码,并爬取源代码中的ID、时间、发送号码、接收号码、发送内容,如果发送内容不为文本,则提取文件所在链接地址,并将爬取的内容写入csv中![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)