我正在使用 BeautifulSoup 使用以下代码解析网站。我能够解析网站并打印数据,但是,我只想打印链接中的一部分数据。任何人都可以就如何做到这一点提供意见吗?
from bs4 import BeautifulSoup as bs
import argparse
import urllib
import urllib2
import getpass
import re
import requests
def update (url):
print url
req = urllib2.Request(url=url)
try:
f = urllib2.urlopen(req)
txt = f.read()
soup = bs(txt)
print soup
f.close()
def main ():
#For logging
print "test"
parser = argparse.ArgumentParser(description='This is the update.py script created by test')
parser.add_argument('-u','--url',action='store',dest='url',default=None,help='<Required> url link',required=True)
results = parser.parse_args()# collect cmd line args
url = results.url
#print url
update(url)
if __name__ == '__main__':
main()
以下是当前输出。预期结果如下所示。
current output :-
==== Test results ====
results are in \\data\loc
==== <font color="#008000">Build Combo</font> ====
{| border="1" cellspacing="1" cellpadding="1"
|-
! bgcolor="#67B0F9" scope="col" | test1
! bgcolor="#67B0F9" scope="col" | test2
! bgcolor="#67B0F9" scope="col" | test3
! bgcolor="#67B0F9" scope="col" | test4
|-
| [http:link.com]
|}
==== <font color="#008000">COde:</font> ====
Expected output:-
==== <font color="#008000">Build Combo</font> ====
{| border="1" cellspacing="1" cellpadding="1"
|-
! bgcolor="#67B0F9" scope="col" | test1
! bgcolor="#67B0F9" scope="col" | test2
! bgcolor="#67B0F9" scope="col" | test3
! bgcolor="#67B0F9" scope="col" | test4
|-
| [http:link.com]
|}