python_读数据采集day1

一、find_all 属性记录
def find_all(self, name=None, attrs={}, recursive=True, text=None,
             limit=None, **kwargs):
name:标签名字
attrs:标签中的属性值 例子:{‘class’:‘split’}
recursive:递归默认为True
text:根据网页内容查询 例子:<p></p>
limit:范围限制参数 limit,显然只用于 findAll 方法。find 其实等价于 findAll 的 limit 等于 1 时的情形。如果你只对网页中获取的前 x 项结果感兴趣,就可以设置它。但是要注意, 这个参数设置之后,获得的前几项结果是按照网页上的顺序排序的,未必是你想要的那 前几项。
kwargs:选择那些具有指定属性的标签 例子:bsObj.findAll(id="text") 和 bsObj.findAll("", {"id":"text"})相同
 
二、Navigablestring 和 Comment 两个对象
from bs4 import Comment
用来表示标签里的文字
from bs4 import NavigableString
用来查找 HTML 文档的注释标签,<!-- 像这样 -->

三 、next_siblings()函数 处理兄弟标签例子table:
处理后的结果不包含第一层tr 的所有tr
选择标签行然后调用 next_siblings,可以选择表 格中除了标题行以外的所有行。
import requests
from bs4 import BeautifulSoup
html = requests.get("http://www.pythonscraping.com/pages/page3.html").text
soup = BeautifulSoup(html,'html.parser')
for  i in soup.find("table",{"id":"giftList"}).tr.next_siblings:
    print(i)

四、基维百科六分割
import requests
import re
from bs4 import BeautifulSoup
import random
import  datetime
#产生随机种子,相同的随机种子容易产生相同的随机数,用时间做随机种子
random.seed(datetime.datetime.now())
def getLink(a):
    r=requests.get('http://en.wikipedia.org'+a)
    print(r.status_code)
    html =r.text
    soup = BeautifulSoup(html,"html.parser")
    #找取a标签
    #/wiki/%E5%87%AF%E6%96%87%C2%B7%E8%B4%9D%E8%82%AF
    links = soup.find('div',id='bodyContent').find_all('a',href=re.compile("^(/wiki/)((?!:).)*$"))
    return links
links = getLink("/wiki/Kevin_Bacon")
while len(links)>0:
    #随机种子产生随机
    print(len(links))
    href = links[random.randint(0,len(links)-1)]['href']
    print(href)
    links = getLink(href)
四、利用set 去重爬取
import requests
import re
from bs4 import BeautifulSoup
pages =set()
def getLinks(pageUrl):
#设置全局变量用外围的set进行存取
    global pages
    r=requests.get("http://en.wikipedia.org"+pageUrl)
    html = r.text
    soup = BeautifulSoup(html,'html.parser')
    for link in soup.find_all("a",href=re.compile("^(/wiki/)")):
        if 'href' in link.attrs:
            if link.attrs['href'] not in pages:
                newPage = link.attrs['href']
                print(newPage)
                pages.add(newPage)
                getLinks(newPage)
getLinks("")

五、针对属性的异常处理
import requests
from bs4 import BeautifulSoup
import re

page = set()
def getLink(pageUrl):
    global page
    r=requests.get("http://en.wikipedia.org"+pageUrl)
    html = r.text
    soup = BeautifulSoup(html,'html.parser')
    #属性的异常处理重点
    try:
        print(soup.h1.get_text())
        print(soup.find(id='mw-content-text').find('p'))
        print(soup.find(id="ca-edit").find("span").find("a").attrs['href'])
    except AttributeError:
        print("缺少属性")

    for links in  soup.findAll("a", href=re.compile("^(/wiki/)")):
        if 'href' in links.attrs:
            if links['href'] not in page:
                newpageurl = links['href']
                page.add(newpageurl)
                getLink(newpageurl)
getLink("")



 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值