forked from yidao620c/python3-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlover.py
More file actions
37 lines (31 loc) · 985 Bytes
/
lover.py
File metadata and controls
37 lines (31 loc) · 985 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 每天一句情话
"""
import requests
import re
from io import StringIO
import json
import xml.etree.ElementTree as ET
def extract_content(xml):
"""xpath解析,或者使用lxml库"""
doc = ET.fromstring(xml)
tt= doc.findall("//div[@class='articleText']")
print(tt)
def lover_sentences_01():
"""获取情话网的情话列表!"""
urls = ['http://www.siandian.com/qinghua/510.html',
'http://www.siandian.com/qinghua/510_2.html',
'http://www.siandian.com/qinghua/1608.html']
for url in urls:
# 读取返回结果
r = requests.get(url)
# 改变r.encoding
encoding = re.search('content="text/html;\s*charset=(.*?)"', r.text).group(1)
r.encoding = encoding
finds = re.finditer(r'<p>\s*(((?!</).)+)\s*</p>', r.text)
for f in finds:
print(f.group(1))
if __name__ == '__main__':
lover_sentences_01()