2017-10-14 114 views
1

刮我有以下代码以提取最新的MS Office版本的Mac:网络与美丽的汤

import urllib2 
from bs4 import BeautifulSoup 

quote_page = 'https://support.office.com/en-us/article/Update-history- 
for-Office-2016-for-Mac-700cab62-0d67-4f23-947b-3686cb1a8eb7#bkmk_current' 
page = urllib2.urlopen(quote_page) 
soup = BeautifulSoup(page, 'html.parser') 
name_box = soup.find('p', attrs={'class': 'x-hidden-focus'}) 
print name_box 

我想刮办公室2016年的Mac(所有应用程序)

15.39。 0

我得到None作为输出。

任何帮助表示赞赏。谢谢。

+3

没有'X-隐藏focus'在源代码中。 –

+0

有趣的是,它看起来像元素只有当你右键点击它们(我想你正在检查元素)时才会得到'x-hidden-focus'类。如果您在没有右击它的情况下导航到另一个“p”,然后右键点击它,您可以看到这是行动。 –

+0

@dang,您的要求完全含糊。你能指定哪一行或表或字符串或任何你想刮? – SIM

回答

0

这个作品,解释在评论中给出。

import requests 
import bs4 

url = 'https://support.office.com/en-us/article/Update-history-for-Office-2016-for-Mac-700cab62-0d67-4f23-947b-3686cb1a8eb7#bkmk_current' 

table_id = 'tblID0EAGAAA' 
resp= requests.get(url) 

soup = bs4.BeautifulSoup(resp.text, 'lxml') 

# find table that contains data of interest 
table = soup.find('table', {'id' : table_id}) 

# get the second row in that table 
second_row = table.findAll('tr')[1] 

# get the second column in that row 
second_column = second_row.findAll('td')[1] 

# get the content in this cell 
version = second_column.find('p').text 

print(version) 
0

不依赖于table id溶液(这非常好每一个版本后,可以改变)或中行的排序:

from bs4 import BeautifulSoup 
import requests 
import re 

page = requests.get('https://support.office.com/en-us/article/Update-history-or-Office-2016-for-Mac-700cab62-0d67-4f23-947b-3686cb1a8eb7#bkmk_current') 
pattern = re.compile(r'^Office.+Mac.*') 

version = BeautifulSoup(page.content, 'html.parser') \ 
      .select_one('section.ocpSection table tbody') \ 
      .find('p', text=pattern) \ 
      .parent \ 
      .find_next_sibling('td') \ 
      .select_one('p') \ 
      .text 
print(version)