python網(wǎng)絡(luò)爬蟲基礎(chǔ)模塊安裝
python的網(wǎng)絡(luò)爬蟲一般需要requests模塊,urllib,urllib2钧萍, urllib3和bs4這幾個模塊褐缠,其中urllib和urllib2在安裝python的時候就已經(jīng)安裝好了,所以直接查找其他模塊即可
https://pan.baidu.com/s/1mhU9RPu(https://github.com/chenyonganyue/cya)這是python的一些包风瘦,找到Lib文件队魏,若安裝模塊的時候提示缺少什么模塊就直接去查找相應(yīng)的文件,并放到python文件夾的lib里面
例如缺少bs4模塊万搔,直接取出bs4開頭的文件
6O$`A}VDU$O$M6W$BEEMQUU.png
1.安裝requests模塊
pip install requests
安裝完成胡桨,打開終端
import requests
若沒報錯,則安裝完成
2.安裝urllib3模塊
pip install urllib3
安裝完成瞬雹,打開終端
import urllib3
若沒報錯昧谊,則安裝完成
3.安裝bs4模塊
pip install bs4
安裝完成,打開終端
import bs4
若沒報錯酗捌,則安裝完成
附上自己寫的一段簡單的代碼:
#coding: utf-8
import requests
import urllib
import re
import time
from bs4 import BeautifulSoup
gjc = urllib.quote('李')
url = 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd='+gjc+'&json=1&p=3&sid=&csor=2&pwd= &cb=jQuery110207361392755424963_1505220177752&_=1505220177757'
proxies = {"http":'http://119.5.0.53', "http":'http://140.250.170.110', "http":'http://221.229.46.81'}
headers = {'GET':url,
'HOST':'sp0.baidu.com',
'Referer':'https://www.baidu.com/?tn=91694651_hao_pg',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0'
}
html = requests.get(url, headers=headers, proxies=proxies).content
soup = BeautifulSoup(html, 'html.parser')
res = soup.get_text()
keyword = re.findall("\"(.*?)\"", res)
num = 0
for i in keyword:
num += 1
if i == "s":
for item in keyword[num:]:
print item