如何通过BeautifulSoup寻求值

如何通过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",{})


但无。请指出一种在标签内查找变量的方法。


  更新:添加了完整代码

最佳答案

这似乎与您的标记一起使用:

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


但是您可能需要首先检查字典中是否存在data-name,因为否则它将引发错误。

关于python - 如何通过BeautifulSoup寻求值(value),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36792179/

10-10 00:27