Python 使用BeautifulSoup导航

Python 使用BeautifulSoup导航,python,html,beautifulsoup,html-parsing,python-requests,Python,Html,Beautifulsoup,Html Parsing,Python Requests,我对如何使用BeautifulSoup导航HTML树有点困惑 import requests from bs4 import BeautifulSoup url = 'http://examplewebsite.com' source = requests.get(url) content = source.content soup = BeautifulSoup(source.content, "html.parser") # Now I navigate the soup for a i

我对如何使用BeautifulSoup导航HTML树有点困惑

import requests
from bs4 import BeautifulSoup

url = 'http://examplewebsite.com'
source = requests.get(url)
content = source.content
soup = BeautifulSoup(source.content, "html.parser")

# Now I navigate the soup
for a in soup.findAll('a'):
    print a.get("href")
  • 是否有办法通过标签仅查找特定的
    href
    ?例如,我想要的所有
    href
    都用特定名称调用,例如在线目录中的
    price

  • 我想要的
    href
    链接都在网页中的某个位置,在页面的。我只能访问这些链接吗

  • 如何将每个
    href
    链接中的内容刮取并保存为文件格式


  • 使用
    BeautifulSoup
    ,这一切都是可行且简单的

    (1) 有没有办法通过标签只找到特定的href?对于 例如,我想要的所有href都用特定的名称调用,例如。 在线目录中的价格

    比如说,您需要的所有链接在文本中都有
    price
    ——您可以使用
    text
    参数:

    soup.find_all("a", text="price")  # text equals to 'price' exactly
    soup.find_all("a", text=lambda text: text and "price" in text)  # 'price' is inside the text
    
    是的,您可以使用和许多其他不同类型的对象来过滤元素,例如,编译:

    如果
    price
    位于“href”属性中的某个位置,则可以使用以下选项:

    或者,通过
    find_all()


    (2) 我想要的href链接都位于 网页,在页面的和一定的范围内。我只能访问这些吗 链接

    当然,找到合适的容器并呼叫:

    或者,您可以按照在具有所需属性或属性值的特定元素中搜索链接的方式编写CSS选择器。例如,这里我们正在搜索
    a
    元素,该元素具有
    href
    属性,位于
    div
    元素中,该元素具有
    容器
    类:

    soup.select("div.container a[href]")
    

    (3) 如何将每个href链接中的内容刮取并保存到中 文件格式

    如果我理解正确,您需要获得适当的链接,遵循它们并将页面的源代码保存到本地HTML文件中。根据您的需求,有多种选择(例如,速度可能非常关键。或者,这只是一项一次性任务,您不关心性能)

    如果您继续使用
    请求
    ,则代码将具有阻塞性质-您将提取链接,跟踪它,保存页面源代码,然后继续下一个链接-其主要缺点是速度较慢(首先取决于有多少链接)。让您开始的示例代码:

    from urlparse import urljoin
    
    from bs4 import BeautifulSoup
    import requests
    
    base_url = 'http://examplewebsite.com'
    with requests.Session() as session:  # maintaining a web-scraping session
        soup = BeautifulSoup(session.get(base_url).content, "html.parser")
    
        for link in soup.select("div.container a[href]"):
            full_link = urljoin(base_url, link["href"])
            title = a.get_text(strip=True)
    
            with open(title + ".html", "w") as f:
                f.write(session.get(full_link).content)
    

    您可以研究或解决该部分。

    (2)您所说的“特定位置”是什么意思。你能摘录一下源文件吗?是“靠近”标签还是什么?(3) 你所说的链接“内容”是什么意思?你想下载链接另一端的文件吗?@RobertB(2)是的,我的意思是在某个
    中有许多
    href
    (3)你点击链接,直到到达数据表。我想刮取这些数据。@Alexe“跟随它们并将页面的源代码保存到本地HTML文件中。”实际上,我需要将每个页面的源代码保存到一个HMTL文件中。因此,将其视为覆盖多个网页的巨大HTML树;我需要一直跟踪链接/子链接,保存这些数据,附加到我的文件中,然后继续。@ShanZhengYang那么听起来你需要在“附加”模式下打开同一个HTML文件,或者只打开一次进行编写,并在跟踪链接和下载页面时保持打开状态。谢谢
    soup.find_all("a", href=lambda href: href and "price" in href)
    
    container = soup.find("div", class_="container")
    for link in container.select("a[href*=price"):
        print(link["href"])
    
    soup.select("div.container a[href]")
    
    from urlparse import urljoin
    
    from bs4 import BeautifulSoup
    import requests
    
    base_url = 'http://examplewebsite.com'
    with requests.Session() as session:  # maintaining a web-scraping session
        soup = BeautifulSoup(session.get(base_url).content, "html.parser")
    
        for link in soup.select("div.container a[href]"):
            full_link = urljoin(base_url, link["href"])
            title = a.get_text(strip=True)
    
            with open(title + ".html", "w") as f:
                f.write(session.get(full_link).content)