网络爬虫入门到实战
创始人
2024-05-06 19:13:32

简介

数据采集文章

开始

入门程序

环境准备

pip3 install beautifulsoup4

基本操作

from urllib.request import urlopen
from bs4 import BeautifulSouphtml = urlopen("http://www.baidu.com")
# print(html.read()) (打印html完整内容)
bsObj = BeautifulSoup(html.read())#选择上面完整内容的a标签
print(bsObj.a)

结果

更具class获取网页信息

得到的元素还可以像操作dom一样得到他们的父节点,兄弟节点等等,也就是可以关系获取信息。

from urllib.request import urlopen
from bs4 import BeautifulSoup# 请求网站数据
html = urlopen("https://www.pythonscraping.com/pages/warandpeace.html")
bsObj = BeautifulSoup(html)
# 根据网站数据得到class为red的元素
name_list = bsObj.find_all("span", {"class": "green"})
for name_item in name_list:# the Empress# print(name_item)#得到最后的名称the Empressprint(name_item.get_text())

结合正则表达式抓取指定图片(淘宝网为例)

这个不能成功

from urllib.request import urlopen
from bs4 import BeautifulSoup
import rehtml = urlopen("https://www.taobao.com/")
bsObj = BeautifulSoup(html)
images = bsObj.find_all("img",{"src": re.compile("\.\.\.webp")})
for image in images:print(image)

获取网站

from urllib.request import urlopen
from bs4 import BeautifulSoup
import rehtml = urlopen("http://en.wikipedia.org/wiki/Kevin_Bacon")
bsObj = BeautifulSoup(html)#得到a标签,并且得到最后的结果
for link in bsObj.find_all("a",href=re.compile("^(/wiki/)((?!:).)*$")):if 'href' in link.attrs:# /wiki/Bernie_Madoffprint(link.attrs['href'])

爬虫实战

相关软件安装

安装requests

pip install requests
python
import requests

 如果能够导入说明安装成功了

安装Selenium

pip  install selenium
python
import selenium

安装ChromeDriver 

下载地址

CNPM Binaries Mirror

https://chromedriver.storage.googleapis.com/index.html

官网

https://sites.google.com/chromium.org/driver/?pli=1

https://sites.google.com/a/chromium.org/chromedriver/downloads

先查看下自己的浏览器版本号

 

找到和自己浏览器支持的版本

在命令行输入
chromedriver

看到上面的说明安装成功 

安装PhantomJS

下载地址

Download PhantomJS

加入环境变量以后打开命令行输入

phantomjs

由于高版本selenium放弃了phantomjs的使用,下面是使用chrome的无界面模式 

from selenium import webdriverchrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get('https://www.baidu.com/')
print("==============")
print(browser.current_url)

aiohttp安装

pip install aiohttp

 lxml安装

pip install lxml

 pyquery安装

tesserocr安装 

Index of /tesseract

pip3 install tesserocr pillow

tornado安装 

pip install  tornado

创建一个简单的访问

import tornado.ioloop
import tornado.web# 每一个handler表示一个请求处理结果class MainHandler(tornado.web.RequestHandler):def get(self):self.write("hello, world")# 下面的r表示访问的路径
def make_app():return tornado.web.Application([(r"/", MainHandler)])if __name__ == "__main__":app = make_app()app.listen(8888)tornado.ioloop.IOLoop.current().start() 

 

Charles安装 

 下载地址

Download a Free Trial of Charles • Charles Web Debugging Proxy

证书配置

 mitmproxy安装

 

Appium安装

 下载地址

https://github.com/appium/appium-desktop/releases

实战

chromedriver

下面是打开百度找到输入框输入python搜索

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWaitbrowser = webdriver.Chrome()
try:browser.get("https://baidu.com")input = browser.find_element(By.ID, "kw")input.send_keys('python')input.send_keys(Keys.ENTER)wait = WebDriverWait(browser, 10)print(browser.current_url)print(browser.get_cookie)print(browser.page_source)
finally:browser.close()

相关内容

热门资讯

应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...