本文最后更新于 2024年5月6日 早上
Requests类库: https://requests.readthedocs.io/en/latest/user/quickstart/
1 2 3 4 5 6 7 8
| url = 'https://httpbin.org/get'
cookies = dict(do_main="cookies") response = request.get(url, params="?a=10&b=20", cookies=cookies)
print("content",response.content) print("cookies", response.cookies) print("JSON", response.json())
|
1 2 3 4 5 6 7 8 9 10 11 12
| url = 'https://httpbin.org/post'
cookies = dict(do_main="cookies") data = {"key": "value"}
response = requests.post(url, data=data, cookies=cookies)
print("content",response.content) print("cookies", response.cookies) print("JSON", response.json())
|
1 2 3 4 5 6 7 8 9 10
| url = 'https://httpbin.org/delete'
cookies = dict(do_main="cookies") data = {"key": "value"}
response = requests.delete(url, data=data, cookies=cookies)
print("content",response.content) print("cookies", response.cookies) print("JSON", response.json())
|
1 2 3 4 5 6 7 8 9 10
| url = 'https://httpbin.org/delete'
cookies = dict(do_main="cookies") data = {"key": "value"}
response = requests.put(url, data=data, cookies=cookies)
print("content",response.content) print("cookies", response.cookies) print("JSON", response.json())
|
下面我们举例说明
当当网 top500
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 38 39 40 41 42 43 44 45 46
| import json import requests import re
def request_dandan(url): try: response = requests.get(url) print(response.content) if response.status_code == 200: return response.text except requests.RequestException: return None
def parse_result(html): pattern = re.compile('<li>.*?list_num.*?(\d+).</div>.*?<img src="(.*?)".*?class="name".*?title="(.*?)">.*?class="star">.*?class="tuijian">(.*?)</span>.*?class="publisher_info">.*?target="_blank">(.*?)</a>.*?class="biaosheng">.*?<span>(.*?)</span></div>.*?<p><span\sclass="price_n">¥(.*?)</span>.*?</li>',re.S) items = re.findall(pattern, html)
for item in items: write_item_to_file(item) yield { 'range': item[0], 'iamge': item[1], 'title': item[2], 'recommend': item[3], 'author': item[4], 'times': item[5], 'price': item[6] }
def write_item_to_file(item): print('开始写入数据 ====> ' + str(item)) with open('book.txt', 'a', encoding='UTF-8') as f: f.write(json.dumps(item, ensure_ascii=False) + '\n') f.close()
def main(page): url = 'http://bang.dangdang.com/books/fivestars/01.00.00.00.00.00-recent30-0-0-1' + str(page) html = request_dandan(url)
parse_result(html)
if __name__ == "__main__": for i in range(1, 26): main(i)
|