如何使用BeautifulSoup

时间:2016-04-22 11:07:24

标签: python html beautifulsoup

我需要从这样的标记中获取人名(Alex Key):

<div class="link_container">
<a class="follow_card" data-uuid="e47443373cfa93d5341ab809f0700b82" 
data-type="person" data-name="Alex Key" data-permalink="/person/alex-acree-2" 
data-image="" data-follower-count="0" href="/person/alex-key-2">Alex Key</a></div>

我尝试代码:

from django.shortcuts import render
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from django.http import HttpResponse
import os

def impTxt(request):
abs_path = os.path.dirname(__file__) # i.e. /path/to/dir/
root_dir = os.path.split(abs_path)[0] #i.e. /path/to/root_of_project/
imp_file_path = "files/links.txt"
abs_imp_file_path = os.path.join(root_dir, imp_file_path) # abs_path to file


with open(abs_imp_file_path, 'r') as inputfile:
    imp_txt = []
    # print imp_txt
    for line in inputfile:
        imp_txt.append(str(line).strip('[]'))
        print line
        # print imp_txt
    for link in imp_txt:
        # print link
        driver = webdriver.Chrome('/Volumes/Storage/downloads_storage/chromedriver')
        driver.get(link)
        driver.set_window_position(0, 0)
        driver.set_window_size(100000, 200000)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(1) 
        soup = BeautifulSoup(driver.page_source, "lxml")
        text = soup.find('a',{'class': 'follow_card'}).getText()
        print text
        # content = {
        # 'text':text,
        # }
        return render(request, "web/parser.html",{})

但是没有。请指出在标记内查找变量的方法。

  

更新:添加完整代码

3 个答案:

答案 0 :(得分:2)

getText()方法可以为您做到:

text = soup.find('a',{'class':'follow_card'}).getText()

这里的班级名称是follow_card

答案 1 :(得分:1)

box-shadow: 0px 1px 2px 0px #505857, 0px 0px 7px 0px #505857;

中指定班级名称
soup.find()

答案 2 :(得分:1)

这似乎适用于您的标记:

text = dict(soup.find('a').attrs)['data-name']

但您可能需要首先检查字典中是否存在data-name,否则会导致错误。