Created
August 31, 2016 11:07
-
-
Save anri-c/7d23489a9400fb02270f21d1488baa07 to your computer and use it in GitHub Desktop.
BeautifulSoup4 メモ ref: http://qiita.com/anri-c/items/65bdc7d75513393a1008
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # list.html | |
| <html> | |
| <head><title></title></head> | |
| <body> | |
| <a href="http://www.example.com/index.html" title="link title a">Example A</a> | |
| <a href="http://wwww.example.org/" title="link title b" target="_blank">Example B</a> | |
| <a href="http://www.example.net/" title="link title c">Example C</a> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from bs4 import BeautifulSoup | |
| soup = BeautifulSoup(open("list.html")) | |
| link = soup.find("a") | |
| print(link["title"]) | |
| # link title a | |
| print(link["href"]) | |
| # http://www.example.com/index.html | |
| print(link.string) | |
| # Example A | |
| link = soup.find("a", target="_blank") | |
| print(link.string) | |
| # Example B | |
| print(link["title"]) | |
| # link title b | |
| print(link["href"]) | |
| # http://wwww.example.org/ | |
| i = [ {"title": x["title"], "url": x["href"], "content": x.string } for x in soup.find_all("a")] | |
| print(i) | |
| # [{'content': 'Example A', 'url': 'http://www.example.com/index.html', 'title': 'link title a'}, {'content': 'Example B', 'url': 'http://wwww.example.org/', 'title': 'link title b'}, {'content': 'Example C', 'url': 'http://www.example.net/', 'title': 'link title c'}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment