【问题标题】:Python | Web Crawlers | Am I using it right? [closed]蟒蛇 |网络爬虫 |我用对了吗? [关闭]
【发布时间】:2017-07-29 21:33:07
【问题描述】:

所以,我现在正在研究 Python,因为我很久以前研究过它,并没有深入学习这些语言,现在,我正在再次研究它。

我现在正在研究的是网络爬虫,但我不确定这是否正确,我认为我应该正在研究这个项目。如果我错了,请纠正我,但这里是我正在考虑的项目

我想编写一个程序,我可以在其中简单地启动它,然后输入一个网站 url(特定的或完整的网站),它会扫描它以查找 Embed/iFrame 代码,并将链接下载到一个表中如:

  • 页面标题 - | -# of iFrame 的 Found- | -嵌入1- -/嵌入1- | -Embed2- -/Embed2- 等等。

我是在寻找正确的语言和方面,还是应该为此寻找其他东西?

提前感谢您的任何反馈/支持!

【问题讨论】:

  • scrapy 是您正在寻找的。​​span>

标签: python iframe embed


【解决方案1】:

有多种方法可以抓取网站。这是一个使用 BeautifulSoup 的示例。
您可以安装BeautifulSoup 使用
pip install python-bs4 for windows
apt-get install python-bs4 for linux

你可以开始here

工作代码

from bs4 import BeautifulSoup
import urllib
r = urllib.urlopen('http://www.aflcio.org/Legislation-and-Politics/Legislative-Alerts').read()
soup = BeautifulSoup(r)
print soup.prettify()[0:1000]

输出:

<class 'bs4.BeautifulSoup'>
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
<!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
<!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="en-US">
 <!--<![endif]-->
 <head>
  <title>
   Access denied | www.aflcio.org used Cloudflare to restrict access
  </title>
  <meta charset="utf-8"/>
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
  <meta content="IE=Edge,chrome=1" http-equiv="X-UA-Compatible"/>
  <meta content="noindex, nofollow" name="robots"/>
  <meta content="width=device-width,initial-scale=1,maximum-scale=1" name="viewport"/>
  <link href="/cdn-cgi/styles/cf.errors.css" id="cf_styles-css" media="screen,projection" rel="stylesheet" type="text/css"/>
  <!--[if lt IE 9]><link rel="stylesheet" id='cf_styles-ie-css' href="/cdn-cgi/styles/cf.errors.ie.css" type="text/css" media="screen,projection" /><![endif]--
>>> 

您可以使用输出来过滤您想要的内容,例如iFrame。 更多详情here.

【讨论】:

  • 太棒了,这正是我想要的,但是当我尝试运行“pip install python-bs4”时,我收到了这个错误:找不到满足python-bs4要求的版本(来自版本:)没有找到与 python-bs4 匹配的发行版(我在 Windows 10 上)编辑:知道了“pip install beautifulsoup4”
  • 很高兴为您提供帮助。如果我的回答解决了你的问题,你介意接受这个答案吗?这样,问题就不会悬而未决
猜你喜欢
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 2020-06-19
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多