import requests
import re
resp = requests.get('https://www.sohu.com/')
pattern = re.compile(r'<a.*?href="(.*?)".*?title="(.*?)".*?>')
if resp.status_code == 200:
print(resp.text)
all_matches = pattern.findall(resp.text)
for href, title in all_matches:
print(href)
print(title)
resp = requests.get('https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png')
with open('baidu.png', 'wb') as file:
file.write(resp.content)
import time
import random
for page in range(1, 11):
resp = requests.get(
url=f'https://movie.douban.com/top250?start={page - 1}',
headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'}
)
pattern1 = re.compile(r'<span class="title">([^&]*?)</span>')
titles = pattern1.findall(resp.text)
pattern2 = re.compile(r'<span class="rating_num".*?>(.*?)</span>')
ranks = pattern2.findall(resp.text)
for title, rank in zip(titles, ranks):
print(title, rank)
time.sleep(random.random() + 1)
|