{"id":1058,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u722c\u866b","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1058.html","title":{"rendered":"Python \u722c\u866b"},"content":{"rendered":"
\n
\u641e\u61c2Python \u722c\u866b\u6293\u53d6\u3001\u5206\u6790\u3001\u5b58\u50a8\u4e09\u8981\u7d20\u548c\u5177\u4f53\u64cd\u4f5c<\/span>\n <\/div>\n \u6293\u53d6<\/span> \u3000\u3000 <\/p>\n <\/body>\u4f55\u8c13\u722c\u866b\uff1f<\/h2>\n
\n \u5b66\u4e60Python\u722c\u866b\u8981\u5177\u5907\u54ea\u4e9b\u57fa\u7840\uff1f
\n Python\u7f51\u7edc\u722c\u866b\u5b66\u4e60\u5efa\u8bae\uff1f
\n Python\u7f51\u7edc\u722c\u866b\u80fd\u5426\u4e00\u5929\u201c\u901f\u6210\u201d\uff1f\n <\/div>\n\u722c\u866b\u4e09\u8981\u7d20<\/h2>\n
\n \u5206\u6790<\/span>
\n \u5b58\u50a8<\/span> <\/p>\n1\u3001urllib<\/h3>\n
import <\/span>urllib.request
# \u7f51\u7ad9\u662fhttps\u8bbf\u95ee\u7684\u9700\u8981\u5f15\u5165ssl\u6a21\u5757<\/span>
import <\/span>ssl
# \u5bfc\u5165ssl\u65f6\u5173\u95ed\u8bc1\u4e66\u9a8c\u8bc1<\/span>
ssl._create_default_https_context = ssl._create_unverified_context
response = urllib.request.urlopen<\/span>('https:\/\/www.lidihuo.com\/python\/spider-test.html'<\/span>)
print(response.read<\/span>().decode<\/span>('utf-8'<\/span>))
<\/span><\/code><\/pre>\n<\/p><\/div>\n <html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<title>python spider <\/title>
<\/head>
<body>
Hello\uff0cI am lidihuo
Welcome to Python spider
<\/body>
<\/html> <\/span><\/code><\/pre>\n<\/p><\/div>\n import <\/span>urllib.request
# \u7f51\u7ad9\u662fhttps\u8bbf\u95ee\u7684\u9700\u8981\u5f15\u5165ssl\u6a21\u5757<\/span>
import <\/span>ssl
# \u5bfc\u5165ssl\u65f6\u5173\u95ed\u8bc1\u4e66\u9a8c\u8bc1<\/span>
ssl._create_default_https_context = ssl._create_unverified_context
url = 'https:\/\/www.lidihuo.com\/python\/spider-test.html'<\/span>
url = url + '?'<\/span> + key + '='<\/span><\/span> + value1 + '&'<\/span> + key2 + '=' + value2
response = urllib.request.urlopen<\/span>(url)
# print(response.read().decode('utf-8'))<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n2\u3001requests<\/h3>\n
import <\/span>requests
# get\u8bf7\u6c42
<\/span> response = requests.get<\/span>(url='https:\/\/www.lidihuo.com\/python\/spider-test.html'<\/span>)
print(response.text)
# \u5e26\u53c2\u6570\u7684requests get\u8bf7\u6c42 # response = requests.get(url='https:\/\/www.lidihuo.com\/python\/spider-test.html', params={'key1':'value1', 'key2':'value2'})<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n <html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<title>python spider <\/title>
<\/head>
<body>
Hello\uff0cI am lidihuo
Welcome to Python spider
<\/body>
<\/html> <\/span><\/code><\/pre>\n<\/p><\/div>\n\u9700\u8981\u767b\u5f55\u7684\u60c5\u51b5\u4e0b<\/h2>\n
1\u3001\u8868\u5355\u63d0\u4ea4\u767b\u5f55<\/h3>\n
params = {'username'<\/span>: 'root'<\/span><\/span>, 'passwd'<\/span>: 'root'}
response = requests.post<\/span>(\"http:xxx.com\/login\"<\/span>, data=params)
for <\/span><\/span>key,value in <\/span>response.cookies.items<\/span>():
print('key = '<\/span>, key + ' |<\/span>||<\/span> value :'+ value)
<\/span><\/code><\/pre>\n<\/p><\/div>\n2\u3001cookie\u767b\u5f55<\/h3>\n
import <\/span>urllib.request
import <\/span>http.cookiejar
import <\/span>http.cookiejar
# \u7f51\u7ad9\u662fhttps\u8bbf\u95ee\u7684\u9700\u8981\u5f15\u5165ssl\u6a21\u5757
<\/span><\/span> import <\/span>ssl
# \u5bfc\u5165ssl\u65f6\u5173\u95ed\u8bc1\u4e66\u9a8c\u8bc1
<\/span><\/span> ssl._create_default_https_context = ssl._create_unverified_context
\"\"\"
\u4fdd\u5b58\u767b\u5f55\u7684cookie
\"\"\"<\/span>
\"\"\"
MozillaCookieJar \uff1a cookiejar\u7684\u5b50\u7c7b
\u4eceFileCookieJar\u6d3e\u751f\u800c\u6765\uff0c\u521b\u5efa\u4e0eMozilla\u6d4f\u89c8\u5668 cookies.txt\u517c\u5bb9\u7684FileCookieJar\u5b9e\u4f8b\u3002
\"\"\"<\/span>
cookie = http.cookiejar.MozillaCookieJar<\/span>('cookie.txt'<\/span><\/span>)
# \u6784\u5efa\u4e00\u4e2acookie\u7684\u5904\u7406\u5668
<\/span> handler = urllib.request.HTTPCookieProcessor<\/span>(cookie)
# \u83b7\u53d6\u4e00\u4e2aopener\u5bf9\u8c61
<\/span> opener = urllib.request.build_opener<\/span>(handler)
# # \u83b7\u53d6\u4e00\u4e2a\u8bf7\u6c42\u5bf9\u8c61
<\/span> request = urllib.request.Request<\/span>('https:\/\/www.lidihuo.com\/python\/spider-test.html'<\/span><\/span>,headers={\"Connection\"<\/span>: \"keep-alive\"<\/span>})
# \u8bf7\u6c42\u670d\u52a1\u5668\uff0c\u83b7\u53d6\u54cd\u5e94\u5bf9\u8c61\u3002cookie\u4f1a\u5728response\u91cc\u4e00\u8d77\u54cd\u5e94
<\/span> response = opener.open<\/span>(request)
# \u4fdd\u5b58cookie\u5230\u6587\u4ef6
<\/span> cookie.save<\/span>(ignore_discard=True, ignore_expires=True)
\"\"\"
\u8bf7\u6c42\u643a\u5e26\u6587\u4ef6\u4e2d\u7684cookie
\"\"\"<\/span>
import <\/span>urllib.request
import <\/span>http.cookiejar
# \u7f51\u7ad9\u662fhttps\u8bbf\u95ee\u7684\u9700\u8981\u5f15\u5165ssl\u6a21\u5757<\/span>
import <\/span>ssl
# \u5bfc\u5165ssl\u65f6\u5173\u95ed\u8bc1\u4e66\u9a8c\u8bc1<\/span>
ssl._create_default_https_context = ssl._create_unverified_context
cookie = http.cookiejar.MozillaCookieJar<\/span>()
cookie.load<\/span>('cookie.txt', ignore_discard=True, ignore_expires=True)
handler = urllib.request.HTTPCookieProcessor<\/span>(cookie)
opener = urllib.request.build_opener<\/span>(handler)
request = urllib.request.Request<\/span>('https:\/\/www.lidihuo.com\/python\/spider-test.html')
html = opener.open<\/span>(request).read<\/span>().decode<\/span>('utf-8'<\/span>)
print(html)
<\/span><\/code><\/pre>\n<\/p><\/div>\n <html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<title>python spider <\/title>
<\/head>
<body>
Hello\uff0cI am lidihuo
Welcome to Python spider
<\/body>
<\/html> <\/span><\/code><\/pre>\n<\/p><\/div>\n\u5e38\u89c1\u7684\u53cd\u722c\u6709\u54ea\u4e9b<\/h2>\n
1\u3001\u901a\u8fc7user-agent\u6765\u63a7\u5236\u8bbf\u95ee<\/h3>\n
# \u8bbe\u7f6e\u8bf7\u6c42\u5934\u4fe1\u606f
<\/span> headers = {
'Host'<\/span>: 'https:\/\/www.lidihuo.com'<\/span>,
'Referer'<\/span>: 'https:\/\/www.lidihuo.com\/python\/spider-test.html'<\/span>,
'User-Agent'<\/span>: 'Mozilla\/5.0 <\/span>(Windows NT 10.0; WOW64) AppleWebKit\/537.36 <\/span>(KHTML, like Gecko) Chrome\/68.0.3440.106 Safari\/537.36'<\/span>
}
response = requests.get<\/span>(\"http:\/\/www.xxxxx.com\"<\/span>, headers=headers)
<\/span><\/code><\/pre>\n<\/p><\/div>\n Accept: text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,image\/apng,*\/*;q=0.8,application\/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Cache-Control: max-age=0
Connection: keep-alive
Cookie: OUTFOX_SEARCH_USER_ID_NCOO=1091135338.9776888; JSESSIONID=FD8597A7CE469BD49E3BB2696FFA92EA
Host: www.lidihuo.com
If-Modified-Since: Mon, 27 Jul 2020 03:42:05 GMT
If-None-Match: W\/\"5f1e4d0d-81dc\"<\/span>
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla\/5.0 <\/span>(Macintosh; Intel Mac OS X 10_13_6) AppleWebKit\/537.36 <\/span>(KHTML, like Gecko) Chrome\/84.0.4147.89 Safari\/537.36
<\/span><\/code><\/pre>\n<\/p><\/div>\n2\u3001\u901a\u8fc7IP\u6765\u9650\u5236<\/h3>\n
proxies = {
\"http\"<\/span>: \"http:\/\/ip\u5730\u5740\"<\/span><\/span>,
\"https\"<\/span>: \"http:\/\/ip\u5730\u5740\",
}
response =requests.get<\/span>(\"http:\/\/www.xxxxx.com\"<\/span>,proxies=random.choices<\/span>(proxies))
<\/span><\/code><\/pre>\n<\/p><\/div>\n3\u3001\u8bbe\u7f6e\u8bf7\u6c42\u95f4\u9694<\/h3>\n
import time
time.sleep(1) <\/span><\/code><\/pre>\n<\/p><\/div>\n4\u3001\u81ea\u52a8\u5316\u6d4b\u8bd5\u5de5\u5177Selenium<\/h3>\n
5\u3001\u53c2\u6570\u901a\u8fc7\u52a0\u5bc6<\/h3>\n
\n
\u8fde\u63a5xxx
\n
\u6216\u8005\u53ef\u4ee5\u4f7f\u7528\"PhantomJS\",PhantomJS\u662f\u4e00\u4e2a\u57fa\u4e8eWebkit\u7684\"\u65e0\u754c\u9762\"(headless)\u3002
\n
\u6d4f\u89c8\u5668\uff0c\u5b83\u4f1a\u628a\u7f51\u7ad9\u52a0\u8f7d\u5230\u5185\u5b58\u5e76\u6267\u884c\u9875\u9762\u4e0a\u7684JavaScript\uff0c\u56e0\u4e3a\u4e0d\u4f1a\u5c55\u793a\u56fe\u5f62\u754c\u9762\uff0c\u6240\u4ee5\u8fd0\u884c\u8d77\u6765\u6bd4\u5b8c\u6574\u7684\u6d4f\u89c8\u5668\u66f4\u9ad8\u6548\u3002\n <\/div>\n6\u3001\u901a\u8fc7robots.txt\u6765\u9650\u5236\u722c\u866b<\/h3>\n
\n
\u53ef\u4ee5\u67e5\u770b\u6dd8\u5b9d\u7684robots.txt\u6587\u4ef6
\n
\u90e8\u5206\u5185\u5bb9\u5982\u4e0b\n <\/div>\n User-agent: Baiduspider
Disallow: \/
User-agent: baiduspider
Disallow: \/ <\/span><\/code><\/pre>\n<\/p><\/div>\n\u5206\u6790<\/h2>\n
\n
xpath()\u80fd\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u6807\u7b7e\uff0c\u5b83\u4f1a\u68c0\u6d4b\u5b57\u7b26\u4e32\u5185\u5bb9\u662f\u5426\u4e3a\u6807\u7b7e\uff0c\u4f46\u662f\u4e0d\u80fd\u68c0\u6d4b\u51fa\u5185\u5bb9\u662f\u5426\u4e3a\u771f\u7684\u6807\u7b7e\u3002
\n
Beautifulsoup\u662fPython\u7684\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u5b83\u7684\u4f5c\u7528\u548c xpath \u4f5c\u7528\u4e00\u6837\uff0c\u90fd\u662f\u7528\u6765\u89e3\u6790html\u6570\u636e\u7684\u76f8\u6bd4\u4e4b\u4e0b\uff0cxpath\u7684\u901f\u5ea6\u4f1a\u5feb\u4e00\u70b9\uff0c\u56e0\u4e3axpath\u5e95\u5c42\u662f\u7528c\u6765\u5b9e\u73b0\u7684\u3002\n <\/div>\n\u5b58\u50a8<\/h2>\n
\n
\u5b58\u50a8\u4e3ajson\u6587\u4ef6\n <\/div>\n import <\/span>json
dictObj = {
'aa'<\/span>:{
'age'<\/span><\/span>: 20,
'city'<\/span><\/span>: 'beijing'<\/span>,
},
'bb'<\/span>: {
'age': 24,
'city': 'shanghai'<\/span>,
}
}
jsObj = json.dumps<\/span>(dictObj, ensure_ascii=False<\/span>)
fileObject = open('jsonFile.json'<\/span>, 'w'<\/span>)
fileObject.write<\/span>(jsObj)
fileObject.close<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n import <\/span>csv
with <\/span>open('student.csv'<\/span>, 'w'<\/span>, newline=''<\/span>) as <\/span>csvfile:
writer = csv.writer<\/span>(csvfile)
writer.writerow<\/span>(['\u59d3\u540d'<\/span>, '\u5e74\u9f84'<\/span>, '\u57ce\u5e02'<\/span>])
writer.writerows<\/span>([['\u5c0f\u660e'<\/span>, 20 , '\u5317\u4eac'<\/span>],['\u6770\u514b'<\/span>, 22, '\u4e0a\u6d77'<\/span>]])
<\/span><\/code><\/pre>\n<\/p><\/div>\n # mongo\u670d\u52a1
<\/span> client = pymongo.MongoClient<\/span>('mongodb:\/\/127.0.0.1:27017\/'<\/span>)
# lidihuo\u6570\u636e\u5e93
<\/span> db = client.lidihuo
# student\u8868,\u6ca1\u6709\u81ea\u52a8\u521b\u5efa
<\/span> student_db = db.student
student_json = {
'name'<\/span>: '\u5c0f\u660e'<\/span>,
'age'<\/span>: 20,
'city'<\/span>: '\u5317\u4eac'<\/span>
}
student_db.insert<\/span>(student_json)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u722c\u866bzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1058","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1058"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1058"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1058\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}