Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python和BeautifulSoup打开页面_Python_Python 2.7_Web Scraping_Beautifulsoup - Fatal编程技术网

Python和BeautifulSoup打开页面

Python和BeautifulSoup打开页面,python,python-2.7,web-scraping,beautifulsoup,Python,Python 2.7,Web Scraping,Beautifulsoup,我想知道如何使用BeautifulSoup打开列表中的另一页?我遵循了,但它没有告诉我们如何打开列表上的另一页。另外,如何打开嵌套在类中的“a href” 这是我的密码: # coding: utf-8 import requests from bs4 import BeautifulSoup r = requests.get("") soup = BeautifulSoup(r.content) soup.find_all("a") for link in soup.find_all("

我想知道如何使用BeautifulSoup打开列表中的另一页?我遵循了,但它没有告诉我们如何打开列表上的另一页。另外,如何打开嵌套在类中的“a href”

这是我的密码:

# coding: utf-8

import requests
from bs4 import BeautifulSoup

r = requests.get("")
soup = BeautifulSoup(r.content)
soup.find_all("a")

for link in soup.find_all("a"):
    print link.get("href")

    for link in soup.find_all("a"):
        print link.text

    for link in soup.find_all("a"):
        print link.text, link.get("href")

    g_data = soup.find_all("div", {"class":"listing__left-column"})

    for item in g_data:
        print item.contents

    for item in g_data:
        print item.contents[0].text
        print link.get('href')

    for item in g_data:
        print item.contents[0]

我正在尝试从每个企业的标题中收集href,然后打开它们并刮取数据。

我仍然不确定您从何处获取HTML,但如果您试图提取所有
href
标记,则以下方法应基于您发布的图像工作:

import requests
from bs4 import BeautifulSoup

r = requests.get("<add your URL here>")
soup = BeautifulSoup(r.content)

for a_tag in soup.find_all('a', class_='listing-name', href=True):
    print 'href: ', a_tag['href']
使用Python2.x进行测试,对于Python3.x,请在print语句中添加括号

  • 我也有同样的问题,我想和大家分享我的发现,因为我确实尝试了这个答案,但由于某些原因,它不起作用,但经过一些研究,我发现了一些有趣的东西

  • 您可能需要找到“href”链接本身的属性: 您将需要确切的,其中包含href链接。在您的案例中,I am thinking=“class”:“listing_uleft-column”并将其等同于一个变量,例如说“all”:


  • 我这样做了,并且我能够进入另一个嵌入在主页中的链接

    首先,我不明白你在问什么。那么,也许你想看看。你需要让我们知道你想刮哪一页。类似于
    r=requests.get(“http://www.yellowpages.com/“”
    将是必需的。我应该对此进行更多的解释,我想做的是在div等中打开a href。我想给每个有链接的href打电话,打开它们的页面,然后开始scrapOk,这样从长时间的阅读中就会发现我想用一些可以做到这一点的东西打开href或类。有人告诉我可以这样做。所以,如果我收到在该页面上打开a href的请求,然后用BS删除该页面,这将有效。谢谢你,他们的页面在删除站点上并不多,他们的页面在侧页中也不多。只是很多关于抓取一页的教程。对于Python和Scraping,您会推荐哪一本书或教程系列。了解HTML的结构是最重要的。你好,马丁,我现在已经得到了我的HTML并提取了数据,但是现在我正在寻找使用beautifulsoup的方法,我们可以有多个具有BS属性的类吗。例如,我已将我的请求_href.content转换为一个变量,现在希望从中提取内容。我知道我不能添加像newpage这样的东西。findAll ectI我建议你通盘考虑,它解释了一切。你可能想点击我答案旁边的勾号,然后你可以提出一个新的问题。你好,你把URL放在哪里了!?
    import requests
    from bs4 import BeautifulSoup
    
    # Configure this to be your first request URL
    r = requests.get("http://www.mywebsite.com/search/")
    soup = BeautifulSoup(r.content)
    
    for a_tag in soup.find_all('a', class_='listing-name', href=True):
        print 'href: ', a_tag['href']
    
    # Configure this to the root of the above website, e.g. 'http://www.mywebsite.com'
    base_url = "http://www.mywebsite.com"
    
    for a_tag in soup.find_all('a', class_='listing-name', href=True):
        print '-' * 60      # Add a line of dashes
        print 'href: ', a_tag['href']
        request_href = requests.get(base_url + a_tag['href'])
        print request_href.content
    
    from bs4 import BeautifulSoup
    all = soup.find_all("div", {"class":"listing__left-column"})
    for item in all:
      for link in item.find_all("a"):
        if 'href' in link.attrs:
            a = link.attrs['href']
            print(a)
            print("")