-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests_regex_maoyan.py
37 lines (27 loc) · 968 Bytes
/
requests_regex_maoyan.py
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
import requests
import re
from requests.exceptions import RequestException
def get_one_page(url):
headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding':'deflate',
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}
try:
response = requests.get(url,headers=headers)
if response.status_code == 200:
return response.text
return None
except RequestException as e:
print("出错了:%s"%(e.message))
return None
def html_parser(html):
pattern = re.compile("<dd>\s*(<i\s*class=.*>.*</i>).*</dd>",re.S)
item = re.findall(pattern,html)
print(item)
def main():
url ='http://maoyan.com/board/4?'
html = get_one_page(url)
html_parser(html)
if __name__ == '__main__':
main()