Get the title and delivery date of Yahoo! News in Python

Hello

Suddenly

```And
```ls
```After that, there was scraping that could be done like cooking for 3 minutes, so I will write it as an article.

### At first

 Since the assistant does not bring "This is a PC with environment settings ~ ~", let's start from the environment settings.

 By the way, my environment is macOS Catalina 10.15, and various apps are often dropped recently due to a lot of bugs.

 Oh, please assume that Python3 is included.

 The module to prepare this time
```beautifulsoup```When```requests```is
 If you don't have either, please install with ``` pip3 install 〇〇```.
 With this, it feels like the ingredients are ready for the time being.
 It's been 3 minutes so far, so let's do our best.

### next
 Open a terminal and do ``` cd destkop``` (this is where you want to save it) and decide where to save it. The recommendation is to create a file called ``` Python``` on your desktop.

 Then create a Python file as ``` vim news.py```. I'm just using vim as a hobby here, so Atom or VS Code is OK.
##### vim is good.

 This time I'm scraping from Yahoo news

#### **`https://news.yahoo.co.jp/Is used as the url.`**
スクリーンショット 2019-11-07 15.29.55.png Show the location of this access ranking, it's getting fun. スクリーンショット 2019-11-07 15.39.34.png Scrap the ``` yjnSub_list``` selected in this photo. Open the developer tools in Chrome and take a look.

code

First of all, if you do not import the module, it will not be talked about, so I will put it in

news.py


import requests
import bs4

Then, assign it to a good feeling such as url or soup, create a function, and print it. It's easy.

news.py


url = 'https://news.yahoo.co.jp'
html = requests.get(url)
soup = bs4.BeautifulSoup(html.text, 'html.parser')
lank = soup.find('ol',class_='yjnSub_list')
urls = list(map(lambda l:l.find('a').get('href'), lank))

def get_title(url):
    html = requests.get(url)
    soup = bs4.BeautifulSoup(html.text, 'html.parser')
    return soup.find('div',class_='hd').text

titles = list(map(get_title, urls))
print('title'.join(titles))

The title of the last `print ('title')` can be rewritten appropriately.

When the code is applied, at the terminal

python3 news.Enter py.


 You should be able to get the news and delivery date and time without any errors.

 If you get a module error, go back to the initial preferences and try installing again with `` `pip3 uninistall 〇〇```.

 Thank you for your hard work.





Recommended Posts

Get the title and delivery date of Yahoo! News in Python
Get the title of yahoo news and analyze sentiment
[Python] Get the main topics of Yahoo News
[Python] Get the update date of a news article from HTML
Get date in Python
Get the caller of a function in Python
Get the X Window System window title in Python
[Python] Get the last updated date of the website
Get the update date of the Python memo file.
How to get the date and time difference in seconds with python
How to get the number of digits in Python
[python] Get the list of classes defined in the module
Get the size (number of elements) of UnionFind in Python
Get the URL of the HTTP redirect destination in Python
Get the last element of the array by splitting the string in Python and PHP
Summary of date processing in Python (datetime and dateutil)
Get the MIME type in Python and determine the file format
Get the number of specific elements in a python list
The story of Python and the story of NaN
Get the desktop path in Python
Get the script path in Python
Get the desktop path in Python
Determine the date and time format in Python and convert to Unixtime
Count the number of Thai and Arabic characters well in Python
Note: Get the first and last items of Python OrderedDict non-destructively
Try to get a list of breaking news threads in Python.
[Python] How to get the first and last days of the month
[python] Get the rank of the values in List in ascending / descending order
Get the number of readers of a treatise on Mendeley in Python
Conversion of string <-> date (date, datetime) in Python
Check the behavior of destructor in Python
Get date and time in specified format
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
In search of the fastest FizzBuzz in Python
Get the EDINET code list in Python
Get rid of DICOM images in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Get a capture of the entire web page in Selenium Python VBA
Comparing the basic grammar of Python and Go in an easy-to-understand manner
Change the saturation and brightness of color specifications like # ff000 in python 2.5
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Get and convert the current time in the system local timezone with python
Open an Excel file in Python and color the map of Japan
Get the number of articles accessed and likes with Qiita API + Python
Get a datetime instance at any time of the day in Python
Get the key for the second layer migration of JSON data in python
Get the contents of git diff from python
Output the number of CPU cores in Python
[Python] Get the files in a folder with Python
Get the weather in Osaka via WebAPI (python)
Summary of the differences between PHP and Python
[Python] Get / edit the scale label of the figure
Match the distribution of each group in Python
The answer of "1/2" is different between python2 and 3
View the result of geometry processing in Python
Specifying the range of ruby and python arrays
Check the date of the flag duty with Python
About the difference between "==" and "is" in python
Find the divisor of the value entered in python