作者:lv | 来源:互联网 | 2023-05-17 13:32
ImlookingtowriteaPythonscript(using3.4.3)thatgrabsaHTMLpagefromaURLandcangothro
I'm looking to write a Python script (using 3.4.3) that grabs a HTML page from a URL and can go through the DOM to try to find a specific element.
我希望编写一个Python脚本(使用3.4.3),该脚本从URL获取HTML页面,并可以遍历DOM以寻找特定的元素。
I currently have this:
我现在:
#!/usr/bin/env python
import urllib.request
def getSite(url):
return urllib.request.urlopen(url)
if __name__ == '__main__':
cOntent= getSite('http://www.google.com').read()
print(content)
When I print content it does print out the entire html page which is something close to what I want... although I would ideally like to be able to navigate through the DOM rather then treating it as a giant string.
当我打印内容时,它会打印出整个html页面这和我想要的很接近。尽管理想情况下,我希望能够在DOM中导航,而不是将其视为一个巨大的字符串。
I'm still fairly new to Python but have experience with multiple other languages (mainly Java, C#, C++, C, PHP, JS). I've done something similar with Java before but wanted to try it out in Python.
我对Python还是比较陌生的,但是有使用多种其他语言(主要是Java、c#、c++、C、PHP、JS)的经验。我以前用Java做过类似的事情,但想在Python中尝试一下。
Any help is appreciated. Cheers!
任何帮助都是感激。干杯!
2 个解决方案