爬虫学习

目前学了的知识点:

  1. requests #请求
  2. BeautifulSoup #获取、筛选静态网页代码数据
  3. pymysql #mysql的库
  4. json #格式
  5. time #time.sleep(1) 等待
  6. urllib #urllib.request 也是请求
  7. selenium #可以获取动态网页的源码、模拟操作网页
  8. re #正则表达式

大概简单流程:

  1. import requests
  2. url = 'http://www.baidu.com'
  3. r = requests.get(url)
  4. print(r.encoding) #编码格式
  5. print(r.apparent_encoding) #另一个编码格式
  6. #部分情况下,若出现中文乱码,则
  7. r.encoding = r.apparent_encoding
  8. print(r.text) #源代码

连接mysql数据库:

  1. import pymysql
  2. try:
  3. db = pymysql.Connect(
  4. host = 'localhost',
  5. port = 3306,
  6. user = 'root',
  7. password = '密码',
  8. db = 'table_name',
  9. charset = 'utf8'
  10. )
  11. cur = db.cursor() #游标
  12. insert = "inset into weather values('%s','%s','%s','%s')"
  13. data = ('a','b','c','d')
  14. cur.execute(insert % data) #这个样子有点特别
  15. db.commit()
  16. except:
  17. print('出错...')
  18. finally:
  19. cur.close()
  20. db.close()

提取数据:

  1. from bs4 import BeautifulSoup
  2. def getone(string):
  3. soup = BeaufitulSoup(string,'lxml') #先把源代码解析为bs对象
  4. index = soup.find_all(attrs={'class','board-index'}) #这种形式,查找出class为board-index的标签
  5. #类似的还有,通过条件查找标签或标签的元素
  6. # 直接根据标签
  7. soup.title
  8. soup.p
  9. soup.a.name
  10. soup.a.parent
  11. # 通过attrs
  12. soup.a.attrs['href'] #获取a标签的href地址
  13. # 通过find,,查找单个
  14. soup.find(id = 'newtitle')
  15. # 通过find_all,,查找多个
  16. soup.find_all(class = 'abc')
  17. soup.find_all(attrs={'class','abc'})
  18. # 通过select
  19. soup.select('a > .main_img')
  20. soup.select('#article_content > div > ul:nth-child(24)') # 可以直接在网页中复制过来
  21. #获取节点信息
  22. atag = soup.find(id = 'new').string

提取动态网站的数据

  1. 使用selenium需要安装这个库,还有相应浏览器的驱动
  1. from selenium import webdriver
  2. from selenium.webdriver.support.wait import WebDriverWait
  3. from selenium.webdriver.support import expected_conditions as EC
  4. from selenium.webdriver.common.by import By
  5. driver = webdriver.Chrome() #加载驱动
  6. driver.get('http://www.baidu.com') #获取源代码
  7. elem = driver.find_element_by_id('new') #通过条件查找元素
  8. #查找条件有:
  9. # 单个查找
  10. find_element_by_id
  11. find_element_by_name
  12. find_element_by_xpath
  13. find_element_by_link_text
  14. find_element_by_partial_link_text
  15. find_element_by_tag_name
  16. find_element_by_class_name
  17. find_element_by_css_selector
  18. # 多个元素,-+s,,,则得到的是集合
  19. find_elements_by_name
  20. find_elements_by_xpath
  21. find_elements_by_link_text
  22. find_elements_by_partial_link_text
  23. find_elements_by_tag_name
  24. find_elements_by_class_name
  25. find_elements_by_css_selector
  26. # 把byxxx放在括号里
  27. find_element(By.ID,'new')
  28. find_elements(By.CLASS_NAME,'abc')
  29. # 获取属性、内容
  30. elem = driver.find_element_by_id('new')
  31. elem.get_attribute('href')
  32. # 等待5秒获得东西
  33. lily = WebDriverWait(driver, 5, 0.5).until(
  34. EC.presence_all_element_located((By.CSS_SELECTOR, ".fcg")))

模拟动作:

  1. browser = webdriver.Chrome()
  2. try:
  3. browser.get('http://www.baidu.com')
  4. ainput = browser.find_element_by_id('kw') #找到id为kw的输入框
  5. ainput.send_keys('python') #在输入框中输入
  6. ainput.send_keys(Kays.ENTER) #按下回车
  7. wait = WebDriverWait(browser,10) #等待10秒
  8. wait.until(EC.presence_of_element_located((By.ID,'content_left')))
  9. print(browser.current_url) #当前网页的链接
  10. print(browser.page_source) #当前网页源代码
  11. except:
  12. print('出错了')
  13. finally:
  14. browser.close()