如何使用 Python 3.5 和 BeautifulSoup 抓取 href
How to scrape href with Python 3.5 and BeautifulSoup
我想从 https://www.kickstarter.com/discover/advanced?category_id=16&woe_id=23424829&sort=magic&seed=2449064&page=1 网站 Python 3.5 和 BeautifulSoup.
抓取每个项目的 href
这是我的代码
#Loading Libraries
import urllib
import urllib.request
from bs4 import BeautifulSoup
#define URL for scraping
theurl = "https://www.kickstarter.com/discover/advanced?category_id=16&woe_id=23424829&sort=magic&seed=2449064&page=1"
thepage = urllib.request.urlopen(theurl)
#Cooking the Soup
soup = BeautifulSoup(thepage,"html.parser")
#Scraping "Link" (href)
project_ref = soup.findAll('h6', {'class': 'project-title'})
project_href = [project.findChildren('a')[0].href for project in project_ref if project.findChildren('a')]
print(project_href)
我得到 [None, None, .... None, None] 回来了。
我需要一个包含 class 中所有 href 的列表。
有什么想法吗?
尝试这样的事情:
import urllib.request
from bs4 import BeautifulSoup
theurl = "https://www.kickstarter.com/discover/advanced?category_id=16&woe_id=23424829&sort=magic&seed=2449064&page=1"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage)
project_href = [i['href'] for i in soup.find_all('a', href=True)]
print(project_href)
这将 return 所有 href
个实例。正如我在您的 link 中看到的那样,很多 href
标签内部都有 #
。您可以使用正确的 links 的简单正则表达式来避免这些,或者忽略 #
符号。
project_href = [i['href'] for i in soup.find_all('a', href=True) if i['href'] != "#"]
这仍然会给你一些像 /discover?ref=nav
这样的垃圾 link,所以如果你想缩小它的范围,请为你需要的 link 使用适当的正则表达式。
编辑:
解决您在评论中提到的问题:
soup = BeautifulSoup(thepage)
for i in soup.find_all('div', attrs={'class' : 'project-card-content'}):
print(i.a['href'])
我想从 https://www.kickstarter.com/discover/advanced?category_id=16&woe_id=23424829&sort=magic&seed=2449064&page=1 网站 Python 3.5 和 BeautifulSoup.
抓取每个项目的 href这是我的代码
#Loading Libraries
import urllib
import urllib.request
from bs4 import BeautifulSoup
#define URL for scraping
theurl = "https://www.kickstarter.com/discover/advanced?category_id=16&woe_id=23424829&sort=magic&seed=2449064&page=1"
thepage = urllib.request.urlopen(theurl)
#Cooking the Soup
soup = BeautifulSoup(thepage,"html.parser")
#Scraping "Link" (href)
project_ref = soup.findAll('h6', {'class': 'project-title'})
project_href = [project.findChildren('a')[0].href for project in project_ref if project.findChildren('a')]
print(project_href)
我得到 [None, None, .... None, None] 回来了。 我需要一个包含 class 中所有 href 的列表。
有什么想法吗?
尝试这样的事情:
import urllib.request
from bs4 import BeautifulSoup
theurl = "https://www.kickstarter.com/discover/advanced?category_id=16&woe_id=23424829&sort=magic&seed=2449064&page=1"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage)
project_href = [i['href'] for i in soup.find_all('a', href=True)]
print(project_href)
这将 return 所有 href
个实例。正如我在您的 link 中看到的那样,很多 href
标签内部都有 #
。您可以使用正确的 links 的简单正则表达式来避免这些,或者忽略 #
符号。
project_href = [i['href'] for i in soup.find_all('a', href=True) if i['href'] != "#"]
这仍然会给你一些像 /discover?ref=nav
这样的垃圾 link,所以如果你想缩小它的范围,请为你需要的 link 使用适当的正则表达式。
编辑:
解决您在评论中提到的问题:
soup = BeautifulSoup(thepage)
for i in soup.find_all('div', attrs={'class' : 'project-card-content'}):
print(i.a['href'])