很抱歉,如果这是重复,但我已经看了很多StackOverflow的问题,但找不到类似的情况。我可能在这里找错了树,但我对编程还不熟悉,所以即使有人能让我走上正确的道路,它也会对我有很大的帮助。
我试图从一个只能从我们的网络内部使用python 3.7和Beautiful 4访问的网站中抓取数据。我的第一个问题是,对于一个新手程序员来说,这是一种最佳实践方法吗?还是我应该研究类似javascript而不是python的东西?
我的第二个问题是网站的根html文件有以下html标记xmlns="http://www.w3.org/1999/xhtml"。BeautifulSoup4是否与xhtml一起工作?“
我承认我对网络开发一无所知,所以即使有人能给我一些关键词或提示,让我开始研究,让我走上一条更有效率的道路,我们还是会很感激的。现在,我最大的问题是,我不知道我不知道什么,所有示例都在更简单的.html页面上工作,而这个页面树是由多个html/css/jpg和gif文件组成的。
谢谢,-Dane
发布于 2018-11-11 17:18:06
Python、请求和BeautifulSoup肯定是可行的,特别是对于初学者来说。BeautifulSoup适用于html、xml等所有变体。
您需要安装python,然后安装请求和bs4。通过阅读请求文档和bs4文档,两者都很容易做到。
如果您还不知道,我建议您学习一些python3的基础知识。
下面是一个获取所需页面标题的简单示例:
import requests
from bs4 import BeautifulSoup as bs
url = 'http://some.local.domain/'
response = requests.get(url)
soup = bs(response.text, 'html.parser')
# let's get title of the page
title = soup.title
print(title)
# let's get all the links in the page
links = soup.find_all('a')
for link in links:
print(link.get('href'))
link1 = link[0]
link2 = link[1]
# let's follow a link we find in the page (we'll go for the first)
response = requests.get(link1, stream=True)
# if we have an image and we want to download it
if response.status_code == 200:
with open(url.split('/')[-1], 'wb') as f:
for chunk in response:
f.write(chunk)
# if the link is another web page
response = requests.get(link2)
soup = bs(response.text, 'html.parser')
# let's get title of the page
title = soup.title
print(title)
继续寻找关于请求的教程,BeautfiulSoup有很多这样的请求.就像这个
https://stackoverflow.com/questions/53254158
复制